-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[finishes #187354254] permissions and role implementation
finishes role and permission assigning functionality modified: package.json new file: src/controllers/permissionController.ts modified: src/controllers/roleControllers.ts new file: src/database/migrations/20240401004514-create-permissions-table.js modified: src/database/migrations/20240413203456-create-role.js renamed: src/database/migrations/20240425195548-create-user.js -> src/database/migrations/20240435195548-create-user.js new file: src/database/migrations/20240505075748-role_permissions.js new file: src/database/models/permission.ts modified: src/database/models/role.ts new file: src/database/models/rolePermissions.ts modified: src/database/models/user.ts modified: src/database/seeders/20240427082911-create-default-role.js new file: src/database/seeders/20240501105101-permissions.js new file: src/database/seeders/20240502080814-create-default-users.js new file: src/database/seeders/20240502093711-role_permissions.js new file: src/docs/permissions.yaml modified: src/middlewares/authMiddlewares.ts modified: src/routes/index.ts new file: src/routes/permissionRoute.ts modified: src/routes/roleRoute.ts new file: src/test/permissionController.test.ts
- Loading branch information
Showing
21 changed files
with
1,048 additions
and
60 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import { Request, Response } from 'express'; | ||
import { sendInternalErrorResponse } from '../validations'; | ||
import Permission from '../database/models/permission'; | ||
import { validateFields } from '../validations/index'; | ||
|
||
// create a permission | ||
export const createPermission = async (req: Request, res: Response): Promise<void> => { | ||
try { | ||
const validFields = validateFields(req, ['name']); | ||
if (validFields.length) { | ||
res.status(400).json({ ok: false, errorMessage: `Missing required fields: ${validFields.join(', ')}` }); | ||
return; | ||
} | ||
const createdPermission = await Permission.create({ name: req.body.name }); | ||
res.status(201).json({ ok: true, data: createdPermission }); | ||
} catch (error) { | ||
sendInternalErrorResponse(res, error); | ||
return; | ||
} | ||
}; | ||
// get all permissions | ||
export const getAllPermissions = async (req: Request, res: Response): Promise<void> => { | ||
try { | ||
const permissions = await Permission.findAll({ attributes: ['id', 'name'] }); | ||
res.status(200).json({ ok: true, data: permissions }); | ||
} catch (error) { | ||
sendInternalErrorResponse(res, error); | ||
return; | ||
} | ||
}; | ||
// get a single permission | ||
export const getSinglePermission = async (req: Request, res: Response): Promise<void> => { | ||
try { | ||
const permission = await Permission.findByPk(req.params.id); | ||
if (!permission) { | ||
res.status(404).json({ ok: false, errorMessage: 'Permission not found' }); | ||
return; | ||
} | ||
res.status(200).json({ ok: true, data: permission }); | ||
} catch (error) { | ||
sendInternalErrorResponse(res, error); | ||
return; | ||
} | ||
}; | ||
// update a permission | ||
export const updatePermission = async (req: Request, res: Response): Promise<void> => { | ||
try { | ||
const validFields = validateFields(req, ['name']); | ||
if (validFields.length) { | ||
res.status(400).json({ ok: false, errorMessage: `Missing required fields: ${validFields.join(', ')}` }); | ||
return; | ||
} | ||
const permissionToUpdate = await Permission.findByPk(req.params.id); | ||
if (!permissionToUpdate) { | ||
res.status(404).json({ ok: false, errorMessage: 'Permission not found' }); | ||
return; | ||
} | ||
if (!req.body.name) res.status(400).json({ ok: false, errorMessage: 'Name is required' }); | ||
|
||
permissionToUpdate.name = req.body.name; | ||
|
||
await permissionToUpdate.save(); | ||
res.status(200).json({ ok: true, data: permissionToUpdate }); | ||
} catch (error) { | ||
sendInternalErrorResponse(res, error); | ||
return; | ||
} | ||
}; | ||
// delete a permission | ||
export const deletePermission = async (req: Request, res: Response): Promise<void> => { | ||
try { | ||
const permissionToDelete = await Permission.findByPk(req.params.id); | ||
if (!permissionToDelete) { | ||
res.status(404).json({ ok: false, errorMessage: 'Permission not found' }); | ||
return; | ||
} | ||
await permissionToDelete.destroy(); | ||
res.status(200).json({ ok: true, message: 'permission deleted successfully!' }); | ||
} catch (error) { | ||
sendInternalErrorResponse(res, error); | ||
return; | ||
} | ||
}; |
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
32 changes: 32 additions & 0 deletions
32
src/database/migrations/20240401004514-create-permissions-table.js
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,32 @@ | ||
'use strict'; | ||
|
||
/** @type {import('sequelize-cli').Migration} */ | ||
module.exports = { | ||
async up(queryInterface, Sequelize) { | ||
await queryInterface.createTable('Permissions', { | ||
id: { | ||
type: Sequelize.INTEGER, | ||
primaryKey: true, | ||
autoIncrement: true, | ||
}, | ||
name: { | ||
type: Sequelize.STRING, | ||
allowNull: false, | ||
defaultValue: 'read', | ||
unique: true, | ||
}, | ||
createdAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE, | ||
}, | ||
updatedAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE, | ||
}, | ||
}); | ||
}, | ||
|
||
async down(queryInterface, Sequelize) { | ||
await queryInterface.dropTable('Permissions'); | ||
}, | ||
}; |
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
40 changes: 40 additions & 0 deletions
40
src/database/migrations/20240505075748-role_permissions.js
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,40 @@ | ||
'use strict'; | ||
|
||
/** @type {import('sequelize-cli').Migration} */ | ||
module.exports = { | ||
async up(queryInterface, Sequelize) { | ||
await queryInterface.createTable('role_permissions', { | ||
roleId: { | ||
type: Sequelize.INTEGER, | ||
allowNull: true, | ||
references: { | ||
model: 'Roles', | ||
key: 'id', | ||
}, | ||
onUpdate: 'CASCADE', | ||
onDelete: 'CASCADE', | ||
}, | ||
permissionId: { | ||
type: Sequelize.INTEGER, | ||
allowNull: true, | ||
references: { | ||
model: 'Permissions', | ||
key: 'id', | ||
}, | ||
onUpdate: 'CASCADE', | ||
onDelete: 'CASCADE', | ||
}, | ||
createdAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE, | ||
}, | ||
updatedAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE, | ||
}, | ||
}); | ||
}, | ||
async down(queryInterface, Sequelize) { | ||
await queryInterface.dropTable('role_permissions'); | ||
}, | ||
}; |
Oops, something went wrong.