File tree Expand file tree Collapse file tree 4 files changed +90
-23
lines changed Expand file tree Collapse file tree 4 files changed +90
-23
lines changed Original file line number Diff line number Diff line change 1
- const productRoutes = require ( './product' ) ;
2
1
const userRoutes = require ( './user' ) ;
3
2
const repositoryRoutes = require ( './repository' ) ;
4
3
const prRoutes = require ( './pull' ) ;
5
4
const roleRoutes = require ( './role' ) ;
6
5
const resourceRoutes = require ( './resource' ) ;
7
6
8
7
const defineRoutes = async ( expressRouter ) => {
9
- productRoutes ( expressRouter ) ;
8
+
10
9
userRoutes ( expressRouter ) ;
11
10
repositoryRoutes ( expressRouter ) ;
12
11
prRoutes ( expressRouter ) ;
Original file line number Diff line number Diff line change @@ -2,42 +2,35 @@ const mongoose = require('mongoose');
2
2
const { baseSchema } = require ( '../../libraries/db/base-schema' ) ;
3
3
4
4
const permissionSchema = new mongoose . Schema ( {
5
- resource : {
6
- type : mongoose . Schema . Types . ObjectId ,
7
- ref : 'Resource' ,
8
- required : true ,
9
- } ,
10
- canAccess : {
11
- type : Boolean ,
12
- default : false ,
13
- } ,
14
- isDisabled : {
15
- type : Boolean ,
16
- default : false ,
5
+ api : {
6
+ type : Array ,
7
+ required : true
17
8
} ,
9
+ client : {
10
+ type : Array ,
11
+ required : false
12
+ }
18
13
} ) ;
19
14
20
15
const roleSchema = new mongoose . Schema ( {
21
16
name : {
22
17
type : String ,
23
18
required : true ,
24
19
unique : true ,
25
- lowercase : true ,
26
20
trim : true ,
27
21
} ,
28
- displayName : {
22
+ identifier : {
29
23
type : String ,
30
24
required : true ,
25
+ unique : true ,
26
+ trim : true ,
27
+ lowercase : true ,
31
28
} ,
32
- description : {
33
- type : String ,
34
- default : '' ,
35
- } ,
36
- permissions : [ permissionSchema ] ,
37
- isSystem : {
29
+ isSystemManaged : {
38
30
type : Boolean ,
39
- default : false ,
31
+ default : true ,
40
32
} ,
33
+ permissions : permissionSchema ,
41
34
} ) ;
42
35
43
36
roleSchema . add ( baseSchema ) ;
Original file line number Diff line number Diff line change
1
+ const Model = require ( '../domains/role/schema' ) ;
2
+ const data = require ( "./files/002-roles.json" ) ;
3
+
4
+ async function insert ( item ) {
5
+ try {
6
+ // check if data already exists by identifier
7
+ const exists = await Model . findOne ( { identifier : item . identifier } ) ;
8
+ if ( exists ) {
9
+ console . log ( `Role already exists: ${ item . identifier } ` ) ;
10
+ return ;
11
+ }
12
+ const result = await Model . create ( item ) ;
13
+ console . log ( `Inserted role: ${ item . _id } ` ) ;
14
+ return result ;
15
+ } catch ( error ) {
16
+ console . error ( `Error inserting role ${ item . _id } :` , error ) ;
17
+ throw error ;
18
+ }
19
+ }
20
+
21
+ async function runMigration ( ) {
22
+ console . log ( "Running migration: 002-add-roles-permissions" ) ;
23
+
24
+ try {
25
+ for ( const role of data . roles ) {
26
+ await insert ( role ) ;
27
+ }
28
+ console . log ( "Successfully completed migration 002" ) ;
29
+ } catch ( error ) {
30
+ console . error ( "Failed to complete migration 002:" , error ) ;
31
+ throw error ;
32
+ }
33
+ }
34
+
35
+ module . exports = { runMigration } ;
Original file line number Diff line number Diff line change
1
+ {
2
+ "roles" : [
3
+ {
4
+ "name" : " Super admin" ,
5
+ "identifier" : " superadmin" ,
6
+ "isSystemManaged" : true ,
7
+ "permissions" : {
8
+ "api" : [
9
+ " /api/*"
10
+ ],
11
+ "client" : []
12
+ }
13
+ },
14
+ {
15
+ "name" : " Admin" ,
16
+ "identifier" : " admin" ,
17
+ "isSystemManaged" : true ,
18
+ "permissions" : {
19
+ "api" : [
20
+ " /api/v1/*" ,
21
+ " /api/users/search" ,
22
+ " /api/users/count" ,
23
+ " /api/users/detail/:id"
24
+ ],
25
+ "client" : []
26
+ }
27
+ },
28
+ {
29
+ "name" : " Visitor" ,
30
+ "identifier" : " visitor" ,
31
+ "isSystemManaged" : true ,
32
+ "permissions" : {
33
+ "api" : [
34
+ " /api/v1/*"
35
+ ],
36
+ "client" : []
37
+ }
38
+ }
39
+ ]
40
+ }
You can’t perform that action at this time.
0 commit comments