Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 73 additions & 50 deletions apps/meteor/app/api/server/v1/roles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { api, Authorization } from '@rocket.chat/core-services';
import type { IRole } from '@rocket.chat/core-typings';
import { Roles, Users } from '@rocket.chat/models';
import { ajv, isRoleAddUserToRoleProps, isRoleDeleteProps, isRoleRemoveUserFromRoleProps } from '@rocket.chat/rest-typings';
import { check, Match } from 'meteor/check';
import { Meteor } from 'meteor/meteor';

import { removeUserFromRolesAsync } from '../../../../server/lib/roles/removeUserFromRoles';
Expand All @@ -29,29 +28,6 @@ API.v1.addRoute(
},
);

API.v1.addRoute(
'roles.sync',
{ authRequired: true },
{
async get() {
check(
this.queryParams,
Match.ObjectIncluding({
updatedSince: Match.Where((value: unknown): value is string => typeof value === 'string' && !Number.isNaN(Date.parse(value))),
}),
);

const { updatedSince } = this.queryParams;

return API.v1.success({
roles: {
update: await Roles.findByUpdatedDate(new Date(updatedSince)).toArray(),
remove: await Roles.trashFindDeletedAfter(new Date(updatedSince)).toArray(),
},
});
},
},
);

API.v1.addRoute(
'roles.addUserToRole',
Expand Down Expand Up @@ -225,42 +201,89 @@ API.v1.addRoute(
},
);

const rolesRoutes = API.v1.get(
'roles.getUsersInPublicRoles',
{
authRequired: true,
response: {
200: ajv.compile<{
users: {
_id: string;
username: string;
roles: string[];
}[];
}>({
type: 'object',
properties: {
const rolesRoutes = API.v1
.get(
'roles.getUsersInPublicRoles',
{
authRequired: true,
response: {
200: ajv.compile<{
users: {
type: 'array',
items: {
type: 'object',
properties: { _id: { type: 'string' }, username: { type: 'string' }, roles: { type: 'array', items: { type: 'string' } } },
_id: string;
username: string;
roles: string[];
}[];
}>({
type: 'object',
properties: {
users: {
type: 'array',
items: {
type: 'object',
properties: { _id: { type: 'string' }, username: { type: 'string' }, roles: { type: 'array', items: { type: 'string' } } },
},
},
},
}),
},
},
async () => {
return API.v1.success({
users: await Authorization.getUsersFromPublicRoles(),
});
},
)
.get(
'roles.sync',
{
authRequired: true,
query: ajv.compile<{ updatedSince: string }>({
type: 'object',
properties: {
updatedSince: {
type: 'string',
},
},
required: ['updatedSince'],
additionalProperties: false,
}),
response: {
200: ajv.compile<{
roles: {
update: IRole[];
remove: object[];
};
}>({
type: 'object',
properties: {
roles: {
type: 'object',
properties: {
update: { type: 'array', items: { type: 'object' } },
remove: { type: 'array', items: { type: 'object' } },
},
required: ['update', 'remove'],
},
},
required: ['roles'],
}),
},
},
},
async function () {
const { updatedSince } = this.queryParams;

async () => {
return API.v1.success({
users: await Authorization.getUsersFromPublicRoles(),
});
},
);
return API.v1.success({
roles: {
update: await Roles.findByUpdatedDate(new Date(updatedSince)).toArray(),
remove: await Roles.trashFindDeletedAfter(new Date(updatedSince)).toArray(),
},
});
},
);

type RolesEndpoints = ExtractRoutesFromAPI<typeof rolesRoutes>;

declare module '@rocket.chat/rest-typings' {
// eslint-disable-next-line @typescript-eslint/naming-convention, @typescript-eslint/no-empty-interface
interface Endpoints extends RolesEndpoints {}
interface Endpoints extends RolesEndpoints { }
}
14 changes: 1 addition & 13 deletions packages/rest-typings/src/v1/roles.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { RocketChatRecordDeleted, IRole, IUserInRole } from '@rocket.chat/core-typings';
import type { IRole, IUserInRole } from '@rocket.chat/core-typings';

import { ajv } from './Ajv';
import type { PaginatedRequest } from '../helpers/PaginatedRequest';
Expand Down Expand Up @@ -113,24 +113,12 @@ const RolesGetUsersInRolePropsSchema = {

export const isRolesGetUsersInRoleProps = ajv.compile<RolesGetUsersInRoleProps>(RolesGetUsersInRolePropsSchema);

type RoleSyncProps = {
updatedSince?: string;
};

export type RolesEndpoints = {
'/v1/roles.list': {
GET: () => {
roles: IRole[];
};
};
'/v1/roles.sync': {
GET: (params: RoleSyncProps) => {
roles: {
update: IRole[];
remove: RocketChatRecordDeleted<IRole>[];
};
};
};

'/v1/roles.addUserToRole': {
POST: (params: RoleAddUserToRoleProps) => {
Expand Down