ToolJet/server/src/services/auth.service.ts

97 lines
3.2 KiB
TypeScript
Raw Normal View History

2021-07-26 16:02:47 +00:00
import { Injectable, NotFoundException, UnauthorizedException } from '@nestjs/common';
import { UsersService } from './users.service';
2021-07-19 05:42:16 +00:00
import { OrganizationsService } from './organizations.service';
2021-07-08 07:39:07 +00:00
import { JwtService } from '@nestjs/jwt';
2021-07-11 05:13:51 +00:00
import { User } from '../entities/user.entity';
2021-07-19 05:42:16 +00:00
import { OrganizationUsersService } from './organization_users.service';
import { EmailService } from './email.service';
import { decamelizeKeys } from 'humps';
2021-07-08 07:39:07 +00:00
const bcrypt = require('bcrypt');
const uuid = require('uuid');
2021-07-08 07:39:07 +00:00
@Injectable()
export class AuthService {
constructor(
private usersService: UsersService,
2021-07-19 05:42:16 +00:00
private jwtService: JwtService,
private organizationsService: OrganizationsService,
private organizationUsersService: OrganizationUsersService,
private emailService: EmailService
) {}
2021-07-08 07:39:07 +00:00
verifyToken(token: string) {
try {
const signedJwt = this.jwtService.verify(token);
return signedJwt;
} catch (err) {
return null;
}
}
2021-07-08 07:39:07 +00:00
async validateUser(email: string, password: string): Promise<User> {
const user = await this.usersService.findByEmail(email);
if (!user) return null;
const isVerified = await bcrypt.compare(password, user.password);
2021-07-08 07:39:07 +00:00
return isVerified ? user : null;
}
async login(params: any) {
const user = await this.validateUser(params.email, params.password);
if (user) {
const payload = { username: user.id, sub: user.email };
return decamelizeKeys({
id: user.id,
2021-07-10 13:54:32 +00:00
auth_token: this.jwtService.sign(payload),
email: user.email,
first_name: user.firstName,
last_name: user.lastName,
Feature: User access management 🔥 (#918) * create migrations for group permissions setup * define new entities and relationships * revise migrations * rename columns * add migration to populate permission groups for existing users * Feature: User access permission group usage (#883) * create migrations for group permissions setup * define new entities and relationships * revise migrations * rename columns * add migration to populate permission groups for existing users * revise migrations * hide roles usage * setup group permissions for apps and users * fix defaultChecked * fix update permission checkbox * fix casl ability check to have params passed * fix casl apps abilities to check with app specific permission * add ability to delete groups * conditionally render edit and delete options for all and admin users * fix user role to group migration * revise group management pages to disallow updating default group * move manage users and groups to navbar dropdown * show only addable apps and users on dropdowns * rename header as profile settings * scope addable apps and users by organization * scope viewable apps on homepage * hide manage groups link from non admins * make permissions to be used with radio input * add loading state for add apps/users buttons * revise unit tests * revise migrations * fix e2e tests * comment out dead code * fix seeds script * handle folder count * captalize error toast * hide manage users dropdown for non admins * show fobidden error on blank homepage * fix folder app count * fix invalid state set * make group name clickable for edit instead * users with edit permission can deploy apps * not show edit link on homepage if user dont have update permission * remove unused entity from merge * remove roles usage from manage org users page * fix folder count and blank slate on homepage * disable add buttons if there is no selections * humanize default groups on view * make app added onto groups have read permission by default * not show app menu if user is not admin * remove admin users from group user addition dropdown * create default permissions for app cloned * fix querying index page without page params * fix admin scoped out from group add * remove apps from header * fix invitation url not shown * scope admin deletion check by org * scope public apps by organization * add specs for group permissions e2e * removed unused entity and add group permissions spec * remove console logs * remove unused permission * scope public app count by org * remove console log * refactor manage group permission resources component * update group permssion in org scope
2021-10-11 15:15:58 +00:00
admin: await this.usersService.hasGroup(user, 'admin'),
group_permissions: await this.usersService.groupPermissions(user),
app_group_permissions: await this.usersService.appGroupPermissions(user),
});
2021-07-08 07:39:07 +00:00
} else {
throw new UnauthorizedException('Invalid credentials');
}
}
2021-07-19 05:42:16 +00:00
async signup(params: any) {
// Check if the installation allows user signups
if (process.env.DISABLE_SIGNUPS === 'true') {
return {};
2021-07-19 05:42:16 +00:00
}
2021-07-19 05:42:16 +00:00
const { email } = params;
const organization = await this.organizationsService.create('Untitled organization');
Feature: User access management 🔥 (#918) * create migrations for group permissions setup * define new entities and relationships * revise migrations * rename columns * add migration to populate permission groups for existing users * Feature: User access permission group usage (#883) * create migrations for group permissions setup * define new entities and relationships * revise migrations * rename columns * add migration to populate permission groups for existing users * revise migrations * hide roles usage * setup group permissions for apps and users * fix defaultChecked * fix update permission checkbox * fix casl ability check to have params passed * fix casl apps abilities to check with app specific permission * add ability to delete groups * conditionally render edit and delete options for all and admin users * fix user role to group migration * revise group management pages to disallow updating default group * move manage users and groups to navbar dropdown * show only addable apps and users on dropdowns * rename header as profile settings * scope addable apps and users by organization * scope viewable apps on homepage * hide manage groups link from non admins * make permissions to be used with radio input * add loading state for add apps/users buttons * revise unit tests * revise migrations * fix e2e tests * comment out dead code * fix seeds script * handle folder count * captalize error toast * hide manage users dropdown for non admins * show fobidden error on blank homepage * fix folder app count * fix invalid state set * make group name clickable for edit instead * users with edit permission can deploy apps * not show edit link on homepage if user dont have update permission * remove unused entity from merge * remove roles usage from manage org users page * fix folder count and blank slate on homepage * disable add buttons if there is no selections * humanize default groups on view * make app added onto groups have read permission by default * not show app menu if user is not admin * remove admin users from group user addition dropdown * create default permissions for app cloned * fix querying index page without page params * fix admin scoped out from group add * remove apps from header * fix invitation url not shown * scope admin deletion check by org * scope public apps by organization * add specs for group permissions e2e * removed unused entity and add group permissions spec * remove console logs * remove unused permission * scope public app count by org * remove console log * refactor manage group permission resources component * update group permssion in org scope
2021-10-11 15:15:58 +00:00
const user = await this.usersService.create({ email }, organization, ['all_users', 'admin']);
// eslint-disable-next-line @typescript-eslint/no-unused-vars
Feature: User access management 🔥 (#918) * create migrations for group permissions setup * define new entities and relationships * revise migrations * rename columns * add migration to populate permission groups for existing users * Feature: User access permission group usage (#883) * create migrations for group permissions setup * define new entities and relationships * revise migrations * rename columns * add migration to populate permission groups for existing users * revise migrations * hide roles usage * setup group permissions for apps and users * fix defaultChecked * fix update permission checkbox * fix casl ability check to have params passed * fix casl apps abilities to check with app specific permission * add ability to delete groups * conditionally render edit and delete options for all and admin users * fix user role to group migration * revise group management pages to disallow updating default group * move manage users and groups to navbar dropdown * show only addable apps and users on dropdowns * rename header as profile settings * scope addable apps and users by organization * scope viewable apps on homepage * hide manage groups link from non admins * make permissions to be used with radio input * add loading state for add apps/users buttons * revise unit tests * revise migrations * fix e2e tests * comment out dead code * fix seeds script * handle folder count * captalize error toast * hide manage users dropdown for non admins * show fobidden error on blank homepage * fix folder app count * fix invalid state set * make group name clickable for edit instead * users with edit permission can deploy apps * not show edit link on homepage if user dont have update permission * remove unused entity from merge * remove roles usage from manage org users page * fix folder count and blank slate on homepage * disable add buttons if there is no selections * humanize default groups on view * make app added onto groups have read permission by default * not show app menu if user is not admin * remove admin users from group user addition dropdown * create default permissions for app cloned * fix querying index page without page params * fix admin scoped out from group add * remove apps from header * fix invitation url not shown * scope admin deletion check by org * scope public apps by organization * add specs for group permissions e2e * removed unused entity and add group permissions spec * remove console logs * remove unused permission * scope public app count by org * remove console log * refactor manage group permission resources component * update group permssion in org scope
2021-10-11 15:15:58 +00:00
const organizationUser = await this.organizationUsersService.create(user, organization);
2021-07-19 05:42:16 +00:00
await this.emailService.sendWelcomeEmail(user.email, user.firstName, user.invitationToken);
2021-07-19 05:42:16 +00:00
return user;
2021-07-26 16:02:47 +00:00
}
async forgotPassword(email: string) {
const user = await this.usersService.findByEmail(email);
const forgotPasswordToken = uuid.v4();
await this.usersService.update(user.id, { forgotPasswordToken });
await this.emailService.sendPasswordResetEmail(email, forgotPasswordToken);
2021-07-26 16:02:47 +00:00
}
async resetPassword(token: string, password: string) {
const user = await this.usersService.findByPasswordResetToken(token);
if (!user) {
throw new NotFoundException('Invalid token');
2021-07-26 16:02:47 +00:00
} else {
await this.usersService.update(user.id, {
Feature: User access management 🔥 (#918) * create migrations for group permissions setup * define new entities and relationships * revise migrations * rename columns * add migration to populate permission groups for existing users * Feature: User access permission group usage (#883) * create migrations for group permissions setup * define new entities and relationships * revise migrations * rename columns * add migration to populate permission groups for existing users * revise migrations * hide roles usage * setup group permissions for apps and users * fix defaultChecked * fix update permission checkbox * fix casl ability check to have params passed * fix casl apps abilities to check with app specific permission * add ability to delete groups * conditionally render edit and delete options for all and admin users * fix user role to group migration * revise group management pages to disallow updating default group * move manage users and groups to navbar dropdown * show only addable apps and users on dropdowns * rename header as profile settings * scope addable apps and users by organization * scope viewable apps on homepage * hide manage groups link from non admins * make permissions to be used with radio input * add loading state for add apps/users buttons * revise unit tests * revise migrations * fix e2e tests * comment out dead code * fix seeds script * handle folder count * captalize error toast * hide manage users dropdown for non admins * show fobidden error on blank homepage * fix folder app count * fix invalid state set * make group name clickable for edit instead * users with edit permission can deploy apps * not show edit link on homepage if user dont have update permission * remove unused entity from merge * remove roles usage from manage org users page * fix folder count and blank slate on homepage * disable add buttons if there is no selections * humanize default groups on view * make app added onto groups have read permission by default * not show app menu if user is not admin * remove admin users from group user addition dropdown * create default permissions for app cloned * fix querying index page without page params * fix admin scoped out from group add * remove apps from header * fix invitation url not shown * scope admin deletion check by org * scope public apps by organization * add specs for group permissions e2e * removed unused entity and add group permissions spec * remove console logs * remove unused permission * scope public app count by org * remove console log * refactor manage group permission resources component * update group permssion in org scope
2021-10-11 15:15:58 +00:00
password,
forgotPasswordToken: null,
});
2021-07-26 16:02:47 +00:00
}
2021-07-19 05:42:16 +00:00
}
2021-07-08 07:39:07 +00:00
}