-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
79410c3
commit d8ac8a1
Showing
19 changed files
with
288 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { RolesBuilder } from "nest-access-control"; | ||
|
||
export enum AppRoles { | ||
AUTHOR = 'AUTHOR', | ||
ADMIN = 'ADMIN' | ||
} | ||
|
||
export enum AppResource { | ||
USER = 'USER', | ||
POST = 'POST' | ||
} | ||
|
||
export const roles: RolesBuilder = new RolesBuilder(); | ||
|
||
roles | ||
// AUTHOR ROLES | ||
.grant(AppRoles.AUTHOR) | ||
.updateOwn([AppResource.USER]) | ||
.deleteOwn([AppResource.USER]) | ||
.createOwn([AppResource.POST]) | ||
.updateOwn([AppResource.POST]) | ||
.deleteOwn([AppResource.POST]) | ||
// ADMIN ROLES | ||
.grant(AppRoles.ADMIN) | ||
.extend(AppRoles.AUTHOR) | ||
.createAny([AppResource.USER]) | ||
.updateAny([AppResource.POST, AppResource.USER]) | ||
.deleteAny([AppResource.POST, AppResource.USER]) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
|
||
|
||
export class LoginDto { | ||
email: string; | ||
password: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,13 @@ | ||
import { applyDecorators, UseGuards } from "@nestjs/common"; | ||
import { ApiBearerAuth } from "@nestjs/swagger"; | ||
import { JwtAuthGuard } from "src/auth/guards"; | ||
import { ACGuard, Role, UseRoles } from "nest-access-control"; | ||
|
||
|
||
export function Auth() { | ||
export function Auth(...roles: Role[]) { | ||
return applyDecorators( | ||
UseGuards(JwtAuthGuard), | ||
UseGuards(JwtAuthGuard, ACGuard), | ||
UseRoles(...roles), | ||
ApiBearerAuth() | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { getRepository } from 'typeorm' | ||
import { ConfigService } from '@nestjs/config' | ||
import { DEFAULT_USER_EMAIL, DEFAULT_USER_PASSWORD } from './constants' | ||
import { User } from 'src/user/entities' | ||
|
||
export const setDefaultUser = async (config: ConfigService) => { | ||
const userRepository = getRepository<User>(User) | ||
|
||
const defaultUser = await userRepository | ||
.createQueryBuilder() | ||
.where('email = :email', { email: config.get<string>('DEFAULT_USER_EMAIL') }) | ||
.getOne() | ||
|
||
if (!defaultUser) { | ||
const adminUser = userRepository.create({ | ||
email: config.get<string>(DEFAULT_USER_EMAIL), | ||
password: config.get<string>(DEFAULT_USER_PASSWORD), | ||
roles: ['ADMIN'] | ||
}) | ||
|
||
return await userRepository.save(adminUser) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from './create-user.dto'; | ||
export * from './edit-user.dto'; | ||
export * from './edit-user.dto'; | ||
export * from './user-registration.dto'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import { OmitType } from "@nestjs/mapped-types"; | ||
import { CreateUserDto } from "./create-user.dto"; | ||
|
||
export class UserRegistrationDto extends OmitType(CreateUserDto, ['roles'] as const) {} |
Oops, something went wrong.