ToolJet/server/src/controllers/organizations.controller.ts

119 lines
4.4 KiB
TypeScript
Raw Normal View History

import { Body, Controller, Get, NotFoundException, Param, Patch, Post, UseGuards, Query } from '@nestjs/common';
2021-07-19 11:30:40 +00:00
import { OrganizationsService } from '@services/organizations.service';
2021-07-19 08:00:42 +00:00
import { decamelizeKeys } from 'humps';
import { User } from 'src/decorators/user.decorator';
2021-07-19 08:00:42 +00:00
import { JwtAuthGuard } from '../../src/modules/auth/jwt-auth.guard';
import { AuthService } from '@services/auth.service';
import { AppAbility } from 'src/modules/casl/casl-ability.factory';
import { CheckPolicies } from 'src/modules/casl/check_policies.decorator';
import { PoliciesGuard } from 'src/modules/casl/policies.guard';
import { User as UserEntity } from 'src/entities/user.entity';
import { ConfigService } from '@nestjs/config';
import { MultiOrganizationGuard } from 'src/modules/auth/multi-organization.guard';
import { OrganizationCreateDto } from '@dto/organization-create.dto';
2021-07-19 08:00:42 +00:00
@Controller('organizations')
export class OrganizationsController {
constructor(
private organizationsService: OrganizationsService,
private authService: AuthService,
private readonly configService: ConfigService
) {}
2021-07-19 08:00:42 +00:00
Merge main to develop (#4049) * Fix: User group permissions error on Openshift platform (#4041) * update dockerfile for file permissions on root group * add permissions from the user group on dockerfile * bump to v1.24.4 * bump to v1.25.0 * [feature] Added pagination and filtering features to users page (#3921) * added pagination and filtering in backend * added pagination - created a seperate component for users table - added pagination * Added filter UI * temporary css fix for pagination footer * fixed pagination width issue * now result will also clear when user clicks on clear icon * Added seperate api for comment mentions * Now we can search mentions by email, first and last names * Fixed a bug - email didn't send for comment mentions * refactoring the code * resolved PR changes * Added isAdmin guard * adding some checks * fixed lint errors * added wild card search * Added no result found text * fixed failing test case * Working on PR changes * Now users table avatars will load image too * replaced skeleton classes with skeleton library component * Completed PR changes * added orderby * Fixed some issues * fixed failed test case * have fixed some css issues * replaced query with quersrting package * fixed minor width issue * Fixed some css issues * fixed darkMode issue * implemented on enter press search * Refactored the code * fixed white space issue * refactored the code * fixed overlapping issue * refactored the code * fixing some issues * fixes * removed guard * code cleanup * comments notification fix * fixed conflict issues * fixed css height issue Co-authored-by: gsmithun4 <gsmithun4@gmail.com> * Remove signup guard from set-password-from-token API (#4050) * Remove sign up guard set-password-from-token API * test cases fix * Bump to v1.25.1 * Feature: Add PG_DB_OWNER env var to disable db and extension creation (#4055) * add PG_DB_OWNER env var to disable db and extension creation * update docs * bump to v1.25.2 Co-authored-by: Akshay <akshaysasidharan93@gmail.com> Co-authored-by: Muhsin Shah C P <muhsinshah21@gmail.com>
2022-09-16 15:38:45 +00:00
@UseGuards(JwtAuthGuard, PoliciesGuard)
@CheckPolicies((ability: AppAbility) => ability.can('viewAllUsers', UserEntity))
2021-07-19 08:00:42 +00:00
@Get('users')
Merge main to develop (#4049) * Fix: User group permissions error on Openshift platform (#4041) * update dockerfile for file permissions on root group * add permissions from the user group on dockerfile * bump to v1.24.4 * bump to v1.25.0 * [feature] Added pagination and filtering features to users page (#3921) * added pagination and filtering in backend * added pagination - created a seperate component for users table - added pagination * Added filter UI * temporary css fix for pagination footer * fixed pagination width issue * now result will also clear when user clicks on clear icon * Added seperate api for comment mentions * Now we can search mentions by email, first and last names * Fixed a bug - email didn't send for comment mentions * refactoring the code * resolved PR changes * Added isAdmin guard * adding some checks * fixed lint errors * added wild card search * Added no result found text * fixed failing test case * Working on PR changes * Now users table avatars will load image too * replaced skeleton classes with skeleton library component * Completed PR changes * added orderby * Fixed some issues * fixed failed test case * have fixed some css issues * replaced query with quersrting package * fixed minor width issue * Fixed some css issues * fixed darkMode issue * implemented on enter press search * Refactored the code * fixed white space issue * refactored the code * fixed overlapping issue * refactored the code * fixing some issues * fixes * removed guard * code cleanup * comments notification fix * fixed conflict issues * fixed css height issue Co-authored-by: gsmithun4 <gsmithun4@gmail.com> * Remove signup guard from set-password-from-token API (#4050) * Remove sign up guard set-password-from-token API * test cases fix * Bump to v1.25.1 * Feature: Add PG_DB_OWNER env var to disable db and extension creation (#4055) * add PG_DB_OWNER env var to disable db and extension creation * update docs * bump to v1.25.2 Co-authored-by: Akshay <akshaysasidharan93@gmail.com> Co-authored-by: Muhsin Shah C P <muhsinshah21@gmail.com>
2022-09-16 15:38:45 +00:00
async getUsers(@User() user, @Query() query) {
const { page, email, firstName, lastName, status } = query;
Merge main to develop (#4049) * Fix: User group permissions error on Openshift platform (#4041) * update dockerfile for file permissions on root group * add permissions from the user group on dockerfile * bump to v1.24.4 * bump to v1.25.0 * [feature] Added pagination and filtering features to users page (#3921) * added pagination and filtering in backend * added pagination - created a seperate component for users table - added pagination * Added filter UI * temporary css fix for pagination footer * fixed pagination width issue * now result will also clear when user clicks on clear icon * Added seperate api for comment mentions * Now we can search mentions by email, first and last names * Fixed a bug - email didn't send for comment mentions * refactoring the code * resolved PR changes * Added isAdmin guard * adding some checks * fixed lint errors * added wild card search * Added no result found text * fixed failing test case * Working on PR changes * Now users table avatars will load image too * replaced skeleton classes with skeleton library component * Completed PR changes * added orderby * Fixed some issues * fixed failed test case * have fixed some css issues * replaced query with quersrting package * fixed minor width issue * Fixed some css issues * fixed darkMode issue * implemented on enter press search * Refactored the code * fixed white space issue * refactored the code * fixed overlapping issue * refactored the code * fixing some issues * fixes * removed guard * code cleanup * comments notification fix * fixed conflict issues * fixed css height issue Co-authored-by: gsmithun4 <gsmithun4@gmail.com> * Remove signup guard from set-password-from-token API (#4050) * Remove sign up guard set-password-from-token API * test cases fix * Bump to v1.25.1 * Feature: Add PG_DB_OWNER env var to disable db and extension creation (#4055) * add PG_DB_OWNER env var to disable db and extension creation * update docs * bump to v1.25.2 Co-authored-by: Akshay <akshaysasidharan93@gmail.com> Co-authored-by: Muhsin Shah C P <muhsinshah21@gmail.com>
2022-09-16 15:38:45 +00:00
const filterOptions = {
...(email && { email }),
...(firstName && { firstName }),
...(lastName && { lastName }),
...(status && { status }),
Merge main to develop (#4049) * Fix: User group permissions error on Openshift platform (#4041) * update dockerfile for file permissions on root group * add permissions from the user group on dockerfile * bump to v1.24.4 * bump to v1.25.0 * [feature] Added pagination and filtering features to users page (#3921) * added pagination and filtering in backend * added pagination - created a seperate component for users table - added pagination * Added filter UI * temporary css fix for pagination footer * fixed pagination width issue * now result will also clear when user clicks on clear icon * Added seperate api for comment mentions * Now we can search mentions by email, first and last names * Fixed a bug - email didn't send for comment mentions * refactoring the code * resolved PR changes * Added isAdmin guard * adding some checks * fixed lint errors * added wild card search * Added no result found text * fixed failing test case * Working on PR changes * Now users table avatars will load image too * replaced skeleton classes with skeleton library component * Completed PR changes * added orderby * Fixed some issues * fixed failed test case * have fixed some css issues * replaced query with quersrting package * fixed minor width issue * Fixed some css issues * fixed darkMode issue * implemented on enter press search * Refactored the code * fixed white space issue * refactored the code * fixed overlapping issue * refactored the code * fixing some issues * fixes * removed guard * code cleanup * comments notification fix * fixed conflict issues * fixed css height issue Co-authored-by: gsmithun4 <gsmithun4@gmail.com> * Remove signup guard from set-password-from-token API (#4050) * Remove sign up guard set-password-from-token API * test cases fix * Bump to v1.25.1 * Feature: Add PG_DB_OWNER env var to disable db and extension creation (#4055) * add PG_DB_OWNER env var to disable db and extension creation * update docs * bump to v1.25.2 Co-authored-by: Akshay <akshaysasidharan93@gmail.com> Co-authored-by: Muhsin Shah C P <muhsinshah21@gmail.com>
2022-09-16 15:38:45 +00:00
};
const usersCount = await this.organizationsService.usersCount(user, filterOptions);
let users = [];
if (usersCount > 0) users = await this.organizationsService.fetchUsers(user, page, filterOptions);
const meta = {
total_pages: Math.ceil(usersCount / 10),
total_count: usersCount,
current_page: parseInt(page || 1),
};
const response = {
meta,
users,
};
return decamelizeKeys(response);
}
@UseGuards(JwtAuthGuard)
@Get('users/suggest')
async getUserSuggestions(@User() user, @Query('input') searchInput) {
const users = await this.organizationsService.fetchUsersByValue(user, searchInput);
const response = {
users,
};
return decamelizeKeys(response);
2021-07-19 08:00:42 +00:00
}
@UseGuards(JwtAuthGuard)
@Get()
async get(@User() user) {
const result = await this.organizationsService.fetchOrganizations(user);
return decamelizeKeys({ organizations: result });
}
@UseGuards(JwtAuthGuard, MultiOrganizationGuard)
@Post()
async create(@User() user, @Body() organizationCreateDto: OrganizationCreateDto) {
const result = await this.organizationsService.create(organizationCreateDto.name, user);
if (!result) {
throw new Error();
}
return await this.authService.switchOrganization(result.id, user, true);
}
@Get(['/:organizationId/public-configs', '/public-configs'])
async getOrganizationDetails(@Param('organizationId') organizationId: string) {
if (!organizationId && this.configService.get<string>('DISABLE_MULTI_WORKSPACE') === 'true') {
// Request from single organization login page - find one from organization and setting
organizationId = (await this.organizationsService.getSingleOrganization())?.id;
}
if (!organizationId) {
throw new NotFoundException();
}
const result = await this.organizationsService.fetchOrganizationDetails(organizationId, [true], true, true);
return decamelizeKeys({ ssoConfigs: result });
}
@UseGuards(JwtAuthGuard, PoliciesGuard)
@CheckPolicies((ability: AppAbility) => ability.can('updateOrganizations', UserEntity))
@Get('/configs')
async getConfigs(@User() user) {
const result = await this.organizationsService.fetchOrganizationDetails(user.organizationId);
return decamelizeKeys({ organizationDetails: result });
}
@UseGuards(JwtAuthGuard, PoliciesGuard)
@CheckPolicies((ability: AppAbility) => ability.can('updateOrganizations', UserEntity))
@Patch()
async update(@Body() body, @User() user) {
await this.organizationsService.updateOrganization(user.organizationId, body);
return {};
}
@UseGuards(JwtAuthGuard, PoliciesGuard)
@CheckPolicies((ability: AppAbility) => ability.can('updateOrganizations', UserEntity))
@Patch('/configs')
async updateConfigs(@Body() body, @User() user) {
const result: any = await this.organizationsService.updateOrganizationConfigs(user.organizationId, body);
return decamelizeKeys({ id: result.id });
}
2021-07-19 08:00:42 +00:00
}