Skip to content

Commit 9f2b44a

Browse files
committed
Fix issue when restoring deleted customer objects
1 parent 371d57e commit 9f2b44a

1 file changed

Lines changed: 10 additions & 4 deletions

File tree

OpenFlow/src/DatabaseConnection.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1354,10 +1354,15 @@ export class DatabaseConnection extends events.EventEmitter {
13541354
// throw new Error("Access denied (not admin or customer admin)");
13551355
}
13561356
if (customer != null) {
1357+
// When restoring deleted items, we need to handle that admins might not exists
13571358
const custadmins = await this.getbyid<Role>(customer.admins, "users", jwt, true, span);
1358-
Base.addRight(item, custadmins._id, custadmins.name, [Rights.full_control]);
1359+
if (custadmins != null) {
1360+
Base.addRight(item, custadmins._id, custadmins.name, [Rights.full_control]);
1361+
} else {
1362+
Base.addRight(item, customer.admins, customer.name + " admins", [Rights.full_control]);
1363+
}
13591364
if (item._id == customer.admins || item._id == customer.users) {
1360-
Base.removeRight(item, custadmins._id, [Rights.delete]);
1365+
Base.removeRight(item, customer.admins, [Rights.delete]);
13611366
}
13621367
(item as any).company = customer.name;
13631368
item = this.ensureResource(item, collectionname);
@@ -2125,8 +2130,9 @@ export class DatabaseConnection extends events.EventEmitter {
21252130

21262131
if (customer != null && !NoderedUtil.IsNullEmpty(user2.customerid) && user2._id != customer.users && user2._id != customer.admins && user2._id != WellknownIds.root) {
21272132
// TODO: Check user has permission to this customer
2128-
const custusers: Role = Role.assign(await this.getbyid<Role>(customer.users, "users", q.jwt, true, span));
2129-
if (!custusers.IsMember(q.item._id)) {
2133+
let custusers: Role = await this.getbyid<Role>(customer.users, "users", q.jwt, true, span);
2134+
if (custusers != null && !custusers.IsMember(q.item._id)) {
2135+
custusers = Role.assign(await this.getbyid<Role>(customer.users, "users", q.jwt, true, span));
21302136
custusers.AddMember(q.item);
21312137
await Logger.DBHelper.Save(custusers, Crypt.rootToken(), span);
21322138
Logger.DBHelper.DeleteKey("user" + q.item._id);

0 commit comments

Comments
 (0)