Skip to content

Commit 4fa55ae

Browse files
committed
fix customerid for jwt token
1 parent 681e3c5 commit 4fa55ae

7 files changed

Lines changed: 81 additions & 26 deletions

File tree

OpenFlow/src/Crypt.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ export class Crypt {
107107
user.name = item.name;
108108
user.username = item.username;
109109
user.roles = item.roles;
110+
user.customerid = item.customerid;
111+
user.selectedcustomerid = item.selectedcustomerid;
110112

111113
const key = Crypt.encryption_key;
112114
if (NoderedUtil.IsNullEmpty(Config.aes_secret)) throw new Exception("Config missing aes_secret");

OpenFlow/src/DatabaseConnection.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -888,7 +888,11 @@ export class DatabaseConnection {
888888
if (!user.HasRoleName("customer admins") && !user.HasRoleName("admins")) throw new Error("Access denied (not admin) to customer with id " + user2.customerid);
889889
customer = await this.getbyid<Customer>(user2.customerid, "users", jwt, span)
890890
if (customer == null) throw new Error("Access denied to customer with id " + user2.customerid);
891-
} if (Config.multi_tenant && !user.HasRoleName("admins")) {
891+
} else if (user.HasRoleName("customer admins") && !NoderedUtil.IsNullEmpty(user.customerid)) {
892+
user2.customerid = user.customerid;
893+
if (!NoderedUtil.IsNullEmpty(user.selectedcustomerid)) user2.customerid = user.selectedcustomerid;
894+
customer = await this.getbyid<Customer>(user2.customerid, "users", jwt, span);
895+
} else if (Config.multi_tenant && !user.HasRoleName("admins")) {
892896
throw new Error("Access denied (not admin or customer admin)");
893897
}
894898
if (customer != null) {
@@ -1286,7 +1290,11 @@ export class DatabaseConnection {
12861290
if (!user.HasRoleName("customer admins") && !user.HasRoleName("admins")) throw new Error("Access denied (not admin) to customer with id " + user2.customerid);
12871291
customer = await this.getbyid<Customer>(user2.customerid, "users", q.jwt, span)
12881292
if (customer == null) throw new Error("Access denied to customer with id " + user2.customerid);
1289-
} if (Config.multi_tenant && !user.HasRoleName("admins")) {
1293+
} else if (user.HasRoleName("customer admins") && !NoderedUtil.IsNullEmpty(user.customerid)) {
1294+
user2.customerid = user.customerid;
1295+
if (!NoderedUtil.IsNullEmpty(user.selectedcustomerid)) user2.customerid = user.selectedcustomerid;
1296+
customer = await this.getbyid<Customer>(user2.customerid, "users", q.jwt, span);
1297+
} else if (Config.multi_tenant && !user.HasRoleName("admins")) {
12901298
throw new Error("Access denied (not admin or customer admin)");
12911299
}
12921300
if (customer != null) {

OpenFlow/src/Messages/Message.ts

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { Readable, Stream } from "stream";
1313
import { GridFSBucket, ObjectID, Cursor } from "mongodb";
1414
import * as path from "path";
1515
import { DatabaseConnection } from "../DatabaseConnection";
16-
import { StripeMessage, EnsureStripeCustomerMessage, NoderedUtil, QueuedMessage, RegisterQueueMessage, QueueMessage, CloseQueueMessage, ListCollectionsMessage, DropCollectionMessage, QueryMessage, AggregateMessage, InsertOneMessage, UpdateOneMessage, Base, UpdateManyMessage, InsertOrUpdateOneMessage, DeleteOneMessage, MapReduceMessage, SigninMessage, TokenUser, User, Rights, EnsureNoderedInstanceMessage, DeleteNoderedInstanceMessage, DeleteNoderedPodMessage, RestartNoderedInstanceMessage, GetNoderedInstanceMessage, GetNoderedInstanceLogMessage, SaveFileMessage, WellknownIds, GetFileMessage, UpdateFileMessage, CreateWorkflowInstanceMessage, RegisterUserMessage, NoderedUser, WatchMessage, GetDocumentVersionMessage, DeleteManyMessage, InsertManyMessage, GetKubeNodeLabels, RegisterExchangeMessage, EnsureCustomerMessage, Customer, stripe_tax_id, Role } from "@openiap/openflow-api";
16+
import { StripeMessage, EnsureStripeCustomerMessage, NoderedUtil, QueuedMessage, RegisterQueueMessage, QueueMessage, CloseQueueMessage, ListCollectionsMessage, DropCollectionMessage, QueryMessage, AggregateMessage, InsertOneMessage, UpdateOneMessage, Base, UpdateManyMessage, InsertOrUpdateOneMessage, DeleteOneMessage, MapReduceMessage, SigninMessage, TokenUser, User, Rights, EnsureNoderedInstanceMessage, DeleteNoderedInstanceMessage, DeleteNoderedPodMessage, RestartNoderedInstanceMessage, GetNoderedInstanceMessage, GetNoderedInstanceLogMessage, SaveFileMessage, WellknownIds, GetFileMessage, UpdateFileMessage, CreateWorkflowInstanceMessage, RegisterUserMessage, NoderedUser, WatchMessage, GetDocumentVersionMessage, DeleteManyMessage, InsertManyMessage, GetKubeNodeLabels, RegisterExchangeMessage, EnsureCustomerMessage, Customer, stripe_tax_id, Role, SelectCustomerMessage } from "@openiap/openflow-api";
1717
import { Billing, stripe_customer, stripe_base, stripe_list, StripeAddPlanMessage, StripeCancelPlanMessage, stripe_subscription, stripe_subscription_item, stripe_plan, stripe_coupon } from "@openiap/openflow-api";
1818
import { V1ResourceRequirements, V1Deployment } from "@kubernetes/client-node";
1919
import { amqpwrapper } from "../amqpwrapper";
@@ -500,6 +500,9 @@ export class Message {
500500
case "ensurecustomer":
501501
await this.EnsureCustomer(cli, span);
502502
break;
503+
case "selectcustomer":
504+
await this.SelectCustomer(cli, span);
505+
break;
503506
case "housekeeping":
504507
this.EnsureJWT(cli);
505508
if (Config.enable_openflow_amqp) {
@@ -3691,8 +3694,6 @@ export class Message {
36913694
}
36923695
this.Send(cli);
36933696
}
3694-
3695-
36963697
// https://dominik.sumer.dev/blog/stripe-checkout-eu-vat
36973698
async EnsureCustomer(cli: WebSocketServerClient, parent: Span) {
36983699
this.Reply();
@@ -4025,6 +4026,44 @@ export class Message {
40254026
}
40264027
Logger.otel.endSpan(span);
40274028
}
4029+
async SelectCustomer(cli: WebSocketServerClient, parent: Span) {
4030+
this.Reply();
4031+
let msg: SelectCustomerMessage;
4032+
try {
4033+
msg = SelectCustomerMessage.assign(this.data);
4034+
if (!NoderedUtil.IsNullEmpty(msg.customerid)) {
4035+
var customer = await Config.db.getbyid<Customer>(msg.customerid, "users", cli.jwt, parent)
4036+
if (customer == null) msg.customerid = null;
4037+
}
4038+
const UpdateDoc: any = { "$set": {} };
4039+
UpdateDoc.$set["selectedcustomerid"] = msg.customerid;
4040+
await Config.db._UpdateOne({ "_id": cli.user._id }, UpdateDoc, "users", 1, false, Crypt.rootToken(), parent);
4041+
cli.user.selectedcustomerid = msg.customerid;
4042+
const tuser: TokenUser = TokenUser.From(cli.user);
4043+
cli.jwt = Crypt.createToken(tuser, Config.shorttoken_expires_in);
4044+
4045+
const l: SigninMessage = new SigninMessage();
4046+
l.jwt = cli.jwt;
4047+
l.user = tuser;
4048+
const m: Message = new Message(); m.command = "refreshtoken";
4049+
m.data = JSON.stringify(l);
4050+
cli.Send(m);
4051+
} catch (error) {
4052+
await handleError(cli, error);
4053+
if (NoderedUtil.IsNullUndefinded(msg)) { (msg as any) = {}; }
4054+
if (msg !== null && msg !== undefined) {
4055+
msg.error = (error.message ? error.message : error);
4056+
}
4057+
}
4058+
try {
4059+
this.data = JSON.stringify(msg);
4060+
} catch (error) {
4061+
this.data = "";
4062+
await handleError(cli, error);
4063+
}
4064+
this.Send(cli);
4065+
}
4066+
40284067
}
40294068

40304069
export class JSONfn {

OpenFlow/src/public/Controllers.ts

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@ export class MenuCtrl {
100100
});
101101
this.$scope.$on('setsearch', (event, data) => {
102102
if (event && data) { }
103-
console.log("setsearch", data)
104103
this.searchstring = data;
105104
});
106105
this.$scope.$on('menurefresh', async (event, data) => {
@@ -141,26 +140,33 @@ export class MenuCtrl {
141140
Search() {
142141
this.$rootScope.$broadcast("search", this.searchstring);
143142
}
144-
SelectCustomer(customer) {
143+
async SelectCustomer(customer) {
145144
if (customer != null) {
146-
if (this.WebSocketClientService.user.customerid == customer._id) {
147-
this.WebSocketClientService.user.selectedcustomerid = null;
148-
} else {
145+
console.log("SelectCustomer " + customer.name, customer)
146+
} else {
147+
console.log("SelectCustomer null", customer)
148+
}
149+
try {
150+
this.customer = customer;
151+
if (customer != null) {
149152
this.WebSocketClientService.user.selectedcustomerid = customer._id;
153+
await NoderedUtil.SelectCustomer(this.WebSocketClientService.user.selectedcustomerid, null, 2);
154+
this.WebSocketClientService.customer = customer as any;
155+
if (this.PathIs("/Customer")) {
156+
this.$location.path("/Customer/" + customer._id);
157+
if (!this.$scope.$$phase) { this.$scope.$apply(); }
158+
}
159+
} else {
160+
this.WebSocketClientService.user.selectedcustomerid = null;
161+
await NoderedUtil.SelectCustomer(this.WebSocketClientService.user.selectedcustomerid, null, 2);
162+
this.WebSocketClientService.customer = null;
150163
}
151-
this.WebSocketClientService.customer = customer as any;
152-
this.$rootScope.$broadcast("menurefresh");
153-
this.$rootScope.$broadcast("search", this.searchstring);
154-
if (this.PathIs("/Customer")) {
155-
this.$location.path("/Customer/" + customer._id);
156-
if (!this.$scope.$$phase) { this.$scope.$apply(); }
157-
}
158-
} else {
159-
this.WebSocketClientService.user.selectedcustomerid = null;
160-
this.WebSocketClientService.customer = null;
161-
this.$rootScope.$broadcast("menurefresh");
164+
// this.$rootScope.$broadcast("menurefresh");
162165
this.$rootScope.$broadcast("search", this.searchstring);
166+
} catch (error) {
167+
console.error(error);
163168
}
169+
if (!this.$scope.$$phase) { this.$scope.$apply(); }
164170
}
165171
}
166172
export class RPAWorkflowCtrl extends entityCtrl<RPAWorkflow> {

OpenFlow/src/public/index.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@
8484
<form class="form-inline d-none d-md-flex ml-auto">
8585
<!-- remove -->
8686
<div ng-class="{active: menuctrl.PathIs('/Customer')}"
87-
ng-show="menuctrl.WebSocketClientService.multi_tenant && menuctrl.customer == null">
87+
ng-show="menuctrl.WebSocketClientService.multi_tenant && menuctrl.user.customerid == null">
8888
<a href="#/Customer" class="nav-link">
8989
<span translate lib="web">create customer</span></a>
9090
</div>
@@ -112,11 +112,11 @@
112112
<div class="dropdown with-arrow"
113113
ng-show="menuctrl.WebSocketClientService.multi_tenant && menuctrl.customers != null && menuctrl.customers.length > 1">
114114
<button class="btn" data-toggle="dropdown" type="button" id="navbar-dropdown-toggle-btn-1">
115-
<span>{{menuctrl.customer.name}}</span>
115+
<span>{{menuctrl.customer.name || "No filter"}}</span>
116116
<i class="fa fa-angle-down" aria-hidden="true"></i>
117117
</button>
118118
<div class="dropdown-menu dropdown-menu-right w-200" aria-labelledby="navbar-dropdown-toggle-btn-1">
119-
<div class="row" ng-show="menuctrl.customer != null">
119+
<div class="row">
120120
<div class="col-sm text-left text-nowrap w-100">
121121
<a href ng-click="menuctrl.SelectCustomer(null)" class="dropdown-item">No filter</a>
122122
</div>

OpenFlowNodeRED/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
},
2626
"dependencies": {
2727
"@nodemailer/mailparser2": "^1.0.3",
28-
"@openiap/openflow-api": "^1.0.91",
28+
"@openiap/openflow-api": "^1.0.92",
2929
"@opentelemetry/api": "^0.18.1",
3030
"@opentelemetry/core": "^0.19.0",
3131
"@opentelemetry/exporter-collector-grpc": "^0.19.0",

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
},
3131
"dependencies": {
3232
"@kubernetes/client-node": "0.14.3",
33-
"@openiap/openflow-api": "^1.0.91",
33+
"@openiap/openflow-api": "^1.0.92",
3434
"@opentelemetry/api": "^0.18.1",
3535
"@opentelemetry/core": "^0.19.0",
3636
"@opentelemetry/exporter-collector-grpc": "^0.19.0",

0 commit comments

Comments
 (0)