1
+ /*
2
+ * @Author : Maslow<[email protected] >
3
+ * @Date : 2021-09-03 23:09:23
4
+ * @LastEditTime : 2021-09-06 14:22:53
5
+ * @Description :
6
+ */
7
+
8
+
9
+ import { Request , Response } from 'express'
10
+ import { ApplicationStruct } from '../../api/application'
11
+ import { checkPermission } from '../../api/permission'
12
+ import { Constants } from '../../constants'
13
+ import { permissions } from '../../constants/permissions'
14
+ import { DatabaseAgent } from '../../lib/db-agent'
15
+ import { hashFunctionCode } from '../../utils/hash'
16
+
17
+ const { POLICY_UPDATE } = permissions
18
+
19
+
20
+ /**
21
+ * Update a policy
22
+ */
23
+ export async function handleUpdatePolicy ( req : Request , res : Response ) {
24
+ const uid = req [ 'auth' ] ?. uid
25
+ const db = DatabaseAgent . sys_db
26
+ const app : ApplicationStruct = req [ 'parsed-app' ]
27
+ const policy_id = req . params . policy_id
28
+
29
+ // check permission
30
+ const code = await checkPermission ( uid , POLICY_UPDATE . name , app )
31
+ if ( code ) {
32
+ return res . status ( code ) . send ( )
33
+ }
34
+
35
+ // get the policy
36
+ const { data : policy } = await db . collection ( Constants . cn . policies )
37
+ . where ( { _id : policy_id , appid : app . appid } )
38
+ . getOne ( )
39
+
40
+ if ( ! policy ) return res . status ( 422 ) . send ( 'policy not found' )
41
+ const body = req . body
42
+
43
+ // build the policy data
44
+ const data = {
45
+ name : body . name ?? policy . name ,
46
+ description : body . description ?? policy . description ,
47
+ status : body . status ?? policy . status ,
48
+ injector : body . injector ?? policy . injector ,
49
+ updated_at : Date . now ( ) ,
50
+ }
51
+
52
+ // do db query
53
+ const ret = await db . collection ( Constants . cn . policies )
54
+ . where ( { appid : app . appid , _id : policy_id } )
55
+ . update ( data )
56
+
57
+ if ( ret . error ) {
58
+ return res . status ( 400 ) . send ( ret . error )
59
+ }
60
+
61
+ return res . send ( {
62
+ data : ret
63
+ } )
64
+ }
65
+
66
+
67
+ /**
68
+ * Update policy rules
69
+ */
70
+ export async function handleUpdatePolicyRules ( req : Request , res : Response ) {
71
+ const uid = req [ 'auth' ] ?. uid
72
+ const db = DatabaseAgent . sys_db
73
+ const app : ApplicationStruct = req [ 'parsed-app' ]
74
+ const policy_id = req . params . policy_id
75
+
76
+ // check permission
77
+ const code = await checkPermission ( uid , POLICY_UPDATE . name , app )
78
+ if ( code ) {
79
+ return res . status ( code ) . send ( )
80
+ }
81
+
82
+ const body = req . body
83
+ if ( ! body . rules ) return res . status ( 422 ) . send ( 'rules cannot be empty' )
84
+
85
+ // get the policy
86
+ const { data : policy } = await db . collection ( Constants . cn . policies )
87
+ . where ( { _id : policy_id , appid : app . appid } )
88
+ . getOne ( )
89
+
90
+ if ( ! policy ) return res . status ( 422 ) . send ( 'policy not found' )
91
+
92
+ // build the policy data
93
+ const data = {
94
+ rules : db . command . set ( body . rules ) ,
95
+ hash : hashFunctionCode ( JSON . stringify ( body . rules ) ) ,
96
+ updated_at : Date . now ( ) ,
97
+ }
98
+
99
+ // do db query
100
+ const ret = await db . collection ( Constants . cn . policies )
101
+ . where ( { appid : app . appid , _id : policy_id } )
102
+ . update ( data )
103
+
104
+ if ( ret . error ) {
105
+ return res . status ( 400 ) . send ( ret . error )
106
+ }
107
+
108
+ return res . send ( {
109
+ data : ret
110
+ } )
111
+ }
0 commit comments