Skip to content

Commit c01f577

Browse files
committed
Remove redundant user and jwt
1 parent 8a23e87 commit c01f577

2 files changed

Lines changed: 59 additions & 61 deletions

File tree

OpenFlow/src/public/Controllers.ts

Lines changed: 42 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,9 @@ export class MenuCtrl {
164164
this.path = this.$location.path();
165165
}
166166
hasrole(role: string) {
167-
if (this.WebSocketClientService.user === null || this.WebSocketClientService.user === undefined) return false;
168-
const hits = this.WebSocketClientService.user.roles.filter(member => member.name == role);
167+
if (WebSocketClient.instance === null || WebSocketClient.instance === undefined) return false;
168+
if (WebSocketClient.instance.user === null || WebSocketClient.instance.user === undefined) return false;
169+
const hits = WebSocketClient.instance.user.roles.filter(member => member.name == role);
169170
return (hits.length == 1)
170171
}
171172
hascordova() {
@@ -189,9 +190,9 @@ export class MenuCtrl {
189190
async EditCustomer(customer) {
190191
try {
191192
if (customer == null) return;
192-
this.WebSocketClientService.user.selectedcustomerid = customer._id;
193+
WebSocketClient.instance.user.selectedcustomerid = customer._id;
193194
this.WebSocketClientService.customer = customer as any;
194-
await NoderedUtil.SelectCustomer(this.WebSocketClientService.user.selectedcustomerid, null, 2);
195+
await NoderedUtil.SelectCustomer(WebSocketClient.instance.user.selectedcustomerid, null, 2);
195196
this.$location.path("/Customer/" + customer._id);
196197
if (!this.$scope.$$phase) { this.$scope.$apply(); }
197198
} catch (error) {
@@ -207,16 +208,16 @@ export class MenuCtrl {
207208
try {
208209
this.customer = customer;
209210
if (customer != null) {
210-
this.WebSocketClientService.user.selectedcustomerid = customer._id;
211-
await NoderedUtil.SelectCustomer(this.WebSocketClientService.user.selectedcustomerid, null, 2);
211+
WebSocketClient.instance.user.selectedcustomerid = customer._id;
212+
await NoderedUtil.SelectCustomer(WebSocketClient.instance.user.selectedcustomerid, null, 2);
212213
this.WebSocketClientService.customer = customer as any;
213214
if (this.PathIs("/Customer")) {
214215
this.$location.path("/Customer/" + customer._id);
215216
if (!this.$scope.$$phase) { this.$scope.$apply(); }
216217
}
217218
} else {
218-
this.WebSocketClientService.user.selectedcustomerid = null;
219-
await NoderedUtil.SelectCustomer(this.WebSocketClientService.user.selectedcustomerid, null, 2);
219+
WebSocketClient.instance.user.selectedcustomerid = null;
220+
await NoderedUtil.SelectCustomer(WebSocketClient.instance.user.selectedcustomerid, null, 2);
220221
this.WebSocketClientService.customer = null;
221222
}
222223
// this.$rootScope.$broadcast("menurefresh");
@@ -1877,10 +1878,10 @@ export class MainCtrl extends entitiesCtrl<Base> {
18771878
// this.basequery = { state: { $ne: "completed" }, $and: [{ form: { $exists: true } }, { form: { "$ne": "none" } }] };
18781879
// this.basequery = { state: { $ne: "completed" }, form: { $exists: true } };
18791880
this.preloadData = () => {
1880-
const user = this.WebSocketClientService.user;
1881+
const user = WebSocketClient.instance.user;
18811882
const ors: any[] = [];
18821883
ors.push({ targetid: user._id });
1883-
this.WebSocketClientService.user.roles.forEach(role => {
1884+
WebSocketClient.instance.user.roles.forEach(role => {
18841885
ors.push({ targetid: role._id });
18851886
});
18861887
this.basequery = {};
@@ -2436,13 +2437,18 @@ export class UserCtrl extends entityCtrl<TokenUser> {
24362437
}
24372438
async processdata() {
24382439
if (this.model != null && (this.model._id != null && this.model._id != "")) {
2439-
this.memberof = await NoderedUtil.Query("users",
2440-
{
2441-
$and: [
2442-
{ _type: "role" },
2443-
{ members: { $elemMatch: { _id: this.model._id } } }
2444-
]
2445-
}, null, { _type: -1, name: 1 }, 5, 0, null, null, null, 2);
2440+
if (this.model._id == WebSocketClient.instance.user._id) {
2441+
console.log(WebSocketClient.instance.user)
2442+
this.memberof = WebSocketClient.instance.user.roles as any;
2443+
} else {
2444+
this.memberof = await NoderedUtil.Query("users",
2445+
{
2446+
$and: [
2447+
{ _type: "role" },
2448+
{ members: { $elemMatch: { _id: this.model._id } } }
2449+
]
2450+
}, null, { _type: -1, name: 1 }, 50, 0, null, null, null, 2);
2451+
}
24462452
} else {
24472453
this.memberof = [];
24482454
}
@@ -2499,12 +2505,12 @@ export class UserCtrl extends entityCtrl<TokenUser> {
24992505
for (let i = 0; i < this.removedmembers.length; i++) {
25002506
const roles = await NoderedUtil.Query("users", { _type: "role", _id: this.removedmembers[i]._id }, null, { _type: -1, name: 1 }, 5, 0, null, null, null, 2);
25012507
if (roles.length > 0) {
2502-
const memberof = this.removedmembers[i];
2503-
if (this.memberof == null || this.memberof == undefined) this.memberof = [];
2504-
const exists = this.memberof.filter(x => x._id == this.model._id);
2508+
const memberof = roles[i];
2509+
const exists = memberof.members.filter(x => x._id == this.model._id);
25052510
if (exists.length > 0) {
25062511
memberof.members = memberof.members.filter(x => x._id != this.model._id);
25072512
try {
2513+
console.log("Updating " + memberof.name, memberof);
25082514
await NoderedUtil.UpdateOne("users", null, memberof, 1, false, null, 2);
25092515
} catch (error) {
25102516
console.error("Error updating " + memberof.name, error);
@@ -2518,6 +2524,7 @@ export class UserCtrl extends entityCtrl<TokenUser> {
25182524
}
25192525
this.$location.path("/Users");
25202526
} catch (error) {
2527+
debugger;
25212528
this.errormessage = error.message ? error.message : error;
25222529
}
25232530
if (!this.$scope.$$phase) { this.$scope.$apply(); }
@@ -3060,7 +3067,7 @@ export class EditFormCtrl extends entityCtrl<Form> {
30603067
// http://www.alpacajs.org/demos/form-builder/form-builder.html
30613068
// https://github.com/kevinchappell/formBuilder - https://formbuilder.online/ - https://kevinchappell.github.io/formBuilder/
30623069
const roles: any = {};
3063-
this.WebSocketClientService.user.roles.forEach(role => {
3070+
WebSocketClient.instance.user.roles.forEach(role => {
30643071
roles[role._id] = role.name;
30653072
});
30663073

@@ -3557,7 +3564,7 @@ export class FormCtrl extends entityCtrl<WorkflowInstance> {
35573564
if (this.form.fbeditor === true) {
35583565
console.debug("renderform");
35593566
const roles: any = {};
3560-
this.WebSocketClientService.user.roles.forEach(role => {
3567+
WebSocketClient.instance.user.roles.forEach(role => {
35613568
roles[role._id] = role.name;
35623569
});
35633570
if (typeof this.form.formData === 'string' || this.form.formData instanceof String) {
@@ -3796,7 +3803,7 @@ export class EntityCtrl extends entityCtrl<Base> {
37963803
for (let i: number = this.keys.length - 1; i >= 0; i--) {
37973804
if (this.keys[i].startsWith('_')) this.keys.splice(i, 1);
37983805
}
3799-
this.searchSelectedItem = WebSocketClientService.user;
3806+
this.searchSelectedItem = WebSocketClient.instance.user;
38003807
this.adduser();
38013808
this.processdata();
38023809
//if (!this.$scope.$$phase) { this.$scope.$apply(); }
@@ -4279,8 +4286,8 @@ export class NoderedCtrl {
42794286
this.loading = true;
42804287
this.userid = $routeParams.id;
42814288
if (this.userid == null || this.userid == undefined || this.userid == "") {
4282-
this.name = WebSocketClientService.user.username;
4283-
this.userid = WebSocketClientService.user._id;
4289+
this.name = WebSocketClient.instance.user.username;
4290+
this.userid = WebSocketClient.instance.user._id;
42844291
const users: NoderedUser[] = await NoderedUtil.Query("users", { _id: this.userid }, null, null, 1, 0, null, null, null, 2);
42854292
if (users.length == 0) {
42864293
this.instancestatus = "Unknown id! " + this.userid;
@@ -5053,7 +5060,7 @@ export class CredentialCtrl extends entityCtrl<Base> {
50535060
this.model = new Base();
50545061
this.model._type = "credential";
50555062
this.model._encrypt = ["password"];
5056-
this.searchSelectedItem = WebSocketClientService.user;
5063+
this.searchSelectedItem = WebSocketClient.instance.user;
50575064
this.adduser();
50585065
}
50595066
});
@@ -5098,7 +5105,7 @@ export class CredentialCtrl extends entityCtrl<Base> {
50985105
ace._id = this.searchSelectedItem._id;
50995106
ace.name = this.searchSelectedItem.name;
51005107
// ace.rights = "//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////8=";
5101-
if (this.WebSocketClientService.user._id != ace._id) {
5108+
if (WebSocketClient.instance.user._id != ace._id) {
51025109
ace.rights = this.unsetBit(ace.rights, 1);
51035110
ace.rights = this.setBit(ace.rights, 2);
51045111
ace.rights = this.unsetBit(ace.rights, 3);
@@ -5827,7 +5834,7 @@ export class CustomerCtrl extends entityCtrl<Customer> {
58275834
if (NoderedUtil.IsNullUndefinded(this.model)) {
58285835
this.model = {} as any;
58295836

5830-
if (this.model.name == null || this.model.name == "") this.model.name = WebSocketClientService.user.name;
5837+
if (this.model.name == null || this.model.name == "") this.model.name = WebSocketClient.instance.user.name;
58315838
this.model._type = "customer";
58325839
var results = await NoderedUtil.Query(this.collection, { "_type": "billing", "userid": user._id }, null, null, 1, 0, null, null, null, 2);
58335840

@@ -5838,15 +5845,15 @@ export class CustomerCtrl extends entityCtrl<Customer> {
58385845
this.model.vatnumber = results[0].vatnumber;
58395846
this.model.vattype = results[0].vattype;
58405847
} else {
5841-
var results = await NoderedUtil.Query(this.collection, { "_type": "user", "_id": WebSocketClientService.user._id }, null, null, 1, 0, null, null, null, 2);
5848+
var results = await NoderedUtil.Query(this.collection, { "_type": "user", "_id": WebSocketClient.instance.user._id }, null, null, 1, 0, null, null, null, 2);
58425849
if (results.length > 0 && !NoderedUtil.IsNullEmpty((results[0] as any).company)) {
58435850
this.model.name = (results[0] as any).company;
58445851
}
58455852
}
5846-
this.model.email = (WebSocketClientService.user as any).username;
5847-
if ((WebSocketClientService.user as any).email) this.model.email = (WebSocketClientService.user as any).email;
5853+
this.model.email = (WebSocketClient.instance.user as any).username;
5854+
if ((WebSocketClient.instance.user as any).email) this.model.email = (WebSocketClient.instance.user as any).email;
58485855
if (this.model.email && this.model.email.indexOf("@") == -1) {
5849-
this.model.email = (WebSocketClientService.user as any).username + "@domain.com";
5856+
this.model.email = (WebSocketClient.instance.user as any).username + "@domain.com";
58505857
}
58515858
console.debug("Create new customer");
58525859
}
@@ -5896,8 +5903,8 @@ export class CustomerCtrl extends entityCtrl<Customer> {
58965903
this.errormessage = "";
58975904
// this.stripe_customer = await NoderedUtil.EnsureStripeCustomer(this.model, this.userid, null, 2);
58985905
if (this.model != null) {
5899-
if (this.WebSocketClientService.user.selectedcustomerid != this.model._id) {
5900-
this.WebSocketClientService.user.selectedcustomerid = this.model._id;
5906+
if (WebSocketClient.instance.user.selectedcustomerid != this.model._id) {
5907+
WebSocketClient.instance.user.selectedcustomerid = this.model._id;
59015908
this.$rootScope.$broadcast("menurefresh");
59025909
}
59035910
}
@@ -6332,7 +6339,7 @@ export class EntityRestrictionCtrl extends entityCtrl<Base> {
63326339
ace._id = this.searchSelectedItem._id;
63336340
ace.name = this.searchSelectedItem.name;
63346341
// ace.rights = "//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////8=";
6335-
if (this.WebSocketClientService.user._id != ace._id) {
6342+
if (WebSocketClient.instance.user._id != ace._id) {
63366343
ace.rights = this.setBit(ace.rights, 1);
63376344
ace.rights = this.unsetBit(ace.rights, 2);
63386345
ace.rights = this.unsetBit(ace.rights, 3);

OpenFlow/src/public/WebSocketClientService.ts

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export class WebSocketClientService {
4040
this.enable_entity_restriction = data.enable_entity_restriction;
4141
this.enable_web_tours = data.enable_web_tours;
4242

43-
if (WebSocketClient.instance == null) {
43+
if (NoderedUtil.IsNullUndefinded(WebSocketClient.instance)) {
4444
const cli: WebSocketClient = new WebSocketClient(this.logger, wsurl);
4545
cli.agent = "webapp";
4646
cli.version = data.version;
@@ -60,16 +60,16 @@ export class WebSocketClientService {
6060
}
6161
loadToken() {
6262
WebSocketClient.instance.getJSON("/jwt", async (error: any, data: any) => {
63-
if (data !== null && data !== undefined) {
64-
if (data.jwt === null || data.jwt === undefined || data.jwt.trim() === "") { data.jwt = null; }
65-
if (data.rawAssertion === null || data.rawAssertion === undefined || data.rawAssertion.trim() === "") { data.rawAssertion = null; }
66-
if (data.jwt === null && data.rawAssertion === null) {
63+
if (!NoderedUtil.IsNullUndefinded(data)) {
64+
if (NoderedUtil.IsNullUndefinded(data.jwt) || data.jwt.trim() === "") { data.jwt = null; }
65+
if (NoderedUtil.IsNullUndefinded(data.rawAssertion) || data.rawAssertion.trim() === "") { data.rawAssertion = null; }
66+
if (NoderedUtil.IsNullUndefinded(data.jwt)) {
6767
console.debug("data.jwt and data.rawAssertion is null");
6868
data = null;
6969
}
7070
}
7171
const _url = this.$location.absUrl();
72-
if (data === null || data === undefined) {
72+
if (NoderedUtil.IsNullUndefinded(data)) {
7373
if (this.$location.path() !== "/Login" && this.$location.path() !== "/Signup") {
7474
// const _url = this.$location.absUrl();
7575
// this.setCookie("weburl", _url, 365);
@@ -81,23 +81,16 @@ export class WebSocketClientService {
8181
}
8282
try {
8383
const result = await NoderedUtil.SigninWithToken(data.jwt, data.rawAssertion, null);
84-
this.user = result.user;
85-
this.jwt = result.jwt;
8684

8785
this.customer = null;
88-
if (!NoderedUtil.IsNullEmpty(this.user.selectedcustomerid)) {
89-
const customers = await NoderedUtil.Query("users", { _type: "customer", "$or": [{ "_id": this.user.selectedcustomerid }, { "_id": this.user.customerid }] }, null, null, 100, 0, null, null, null, 2);
90-
if (customers.length > 0 && (this.user.selectedcustomerid != null)) {
91-
if (this.user.selectedcustomerid != null) {
86+
if (!NoderedUtil.IsNullEmpty(WebSocketClient.instance.user.selectedcustomerid)) {
87+
const customers = await NoderedUtil.Query("users", { _type: "customer", "$or": [{ "_id": WebSocketClient.instance.user.selectedcustomerid }, { "_id": WebSocketClient.instance.user.customerid }] }, null, null, 100, 0, null, null, null, 2);
88+
if (customers.length > 0 && (WebSocketClient.instance.user.selectedcustomerid != null)) {
89+
if (WebSocketClient.instance.user.selectedcustomerid != null) {
9290
for (let cust of customers)
93-
if (cust._id == this.user.selectedcustomerid) this.customer = cust;
91+
if (cust._id == WebSocketClient.instance.user.selectedcustomerid) this.customer = cust;
9492
}
9593
}
96-
// if (this.customer == null && customers.length > 0) {
97-
// for (let cust of customers)
98-
// if (cust._id == this.user.customerid) this.customer = cust;
99-
100-
// }
10194
}
10295

10396
this.$rootScope.$broadcast("signin", result.user);
@@ -126,9 +119,7 @@ export class WebSocketClientService {
126119
}
127120
async impersonate(userid: string) {
128121
try {
129-
const result = await NoderedUtil.SigninWithToken(this.jwt, null, userid);
130-
this.user = result.user;
131-
this.jwt = result.jwt;
122+
const result = await NoderedUtil.SigninWithToken(WebSocketClient.instance.jwt, null, userid);
132123
this.$rootScope.$broadcast("signin", result.user);
133124
} catch (error) {
134125
console.error(error);
@@ -166,8 +157,8 @@ export class WebSocketClientService {
166157
silly(msg) { console.debug(msg); }
167158
}
168159
public customer: Customer = null;
169-
public user: TokenUser = null;
170-
public jwt: string = null;
160+
// public user: TokenUser = null;
161+
// public jwt: string = null;
171162
public version: string = "";
172163
public messageQueue: IHashTable<QueuedMessage> = {};
173164
public usingCordova: boolean = false;
@@ -201,14 +192,14 @@ export class WebSocketClientService {
201192
xhr.send();
202193
}
203194
public onSignedin(callback: onSignedinCallback) {
204-
if (this.user !== null) {
205-
callback(this.user);
195+
if (!NoderedUtil.IsNullUndefinded(WebSocketClient.instance.user)) {
196+
callback(WebSocketClient.instance.user);
206197
return;
207198
}
208199
const cleanup = this.$rootScope.$on('signin', (event, data) => {
209200
if (event && data) { }
210201
cleanup();
211-
callback(this.user);
202+
callback(WebSocketClient.instance.user);
212203
});
213204
}
214205
onConnected(callback) {

0 commit comments

Comments
 (0)