Skip to content

Commit

Permalink
fix: delete org user (#8742)
Browse files Browse the repository at this point in the history
Signed-off-by: mertmit <mertmit99@gmail.com>
  • Loading branch information
mertmit committed Jun 14, 2024
1 parent 70b3139 commit aa7b9a1
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 52 deletions.
29 changes: 0 additions & 29 deletions packages/nocodb/src/meta/meta.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,35 +326,6 @@ export class MetaService {
return query.del();
}

/***
* Delete meta data with condition (USE WITH CAUTION)
* @param target - Table name
* @param idOrCondition - If string, will delete the record with the given id. If object, will delete the record with the given condition.
* @param xcCondition - Additional nested or complex condition to be added to the query.
*/
public async metaDeleteAll(
target: string,
idOrCondition: string | { [p: string]: any },
xcCondition?: Condition,
): Promise<void> {
const query = this.knexConnection(target);

if (typeof idOrCondition !== 'object') {
query.where('id', idOrCondition);
} else if (idOrCondition) {
query.where(idOrCondition);
}

if (xcCondition) {
query.condition(xcCondition, {});
}

// Check if a condition is present in the query builder and throw an error if not.
this.checkConditionPresent(query, 'delete');

return query.del();
}

/***
* Get meta data
* @param workspace_id - Workspace id
Expand Down
15 changes: 6 additions & 9 deletions packages/nocodb/src/models/BaseUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import {
CacheGetType,
CacheScope,
MetaTable,
RootScopes,
} from '~/utils/globals';
import Noco from '~/Noco';
import NocoCache from '~/cache/NocoCache';
import { extractProps } from '~/helpers/extractProps';
import { parseMetaProp } from '~/utils/modelUtils';
import { NcError } from '~/helpers/catchError';

export default class BaseUser {
fk_workspace_id?: string;
Expand Down Expand Up @@ -359,14 +359,11 @@ export default class BaseUser {
userId: string,
ncMeta = Noco.ncMeta,
): Promise<BaseUser[]> {
return await ncMeta.metaList2(
RootScopes.BASE,
RootScopes.BASE,
MetaTable.PROJECT_USERS,
{
condition: { fk_user_id: userId },
},
);
if (!userId) NcError.badRequest('User Id is required');

return await ncMeta.knex(MetaTable.PROJECT_USERS).where({
fk_user_id: userId,
});
}

static async getProjectsList(
Expand Down
17 changes: 7 additions & 10 deletions packages/nocodb/src/models/SyncSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,17 +150,14 @@ export default class SyncSource {
);
}

static async deleteByUserId(
context: NcContext,
userId: string,
ncMeta = Noco.ncMeta,
) {
throw new Error('Method not implemented.');

static async deleteByUserId(userId: string, ncMeta = Noco.ncMeta) {
if (!userId) NcError.badRequest('User Id is required');

return await ncMeta.metaDeleteAll(MetaTable.SYNC_SOURCE, {
fk_user_id: userId,
});
return await ncMeta
.knex(MetaTable.SYNC_SOURCE)
.where({
fk_user_id: userId,
})
.del();
}
}
6 changes: 2 additions & 4 deletions packages/nocodb/src/services/org-users.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { validatePayload } from '~/helpers';
import { NcError } from '~/helpers/catchError';
import { extractProps } from '~/helpers/extractProps';
import { randomTokenString } from '~/helpers/stringHelpers';
import { BaseUser, Store, User } from '~/models';
import { BaseUser, Store, SyncSource, User } from '~/models';

import Noco from '~/Noco';
import { MetaTable, RootScopes } from '~/utils/globals';
Expand Down Expand Up @@ -65,7 +65,6 @@ export class OrgUsersService {
}

// delete base user entry and assign to super admin
// TODO-TENANT: scope this to org
const baseUsers = await BaseUser.getProjectsIdList(param.userId, ncMeta);

// todo: clear cache
Expand All @@ -84,8 +83,7 @@ export class OrgUsersService {
}

// delete sync source entry
// TODO-TENANT: ENABLE THIS & CONFIRM SCOPE
// await SyncSource.deleteByUserId(param.userId, ncMeta);
await SyncSource.deleteByUserId(param.userId, ncMeta);

// delete user
await User.delete(param.userId, ncMeta);
Expand Down

0 comments on commit aa7b9a1

Please sign in to comment.