@@ -2,6 +2,7 @@ import { isNil } from 'lodash';
2
2
import { Inject } from 'noicejs' ;
3
3
import { Connection , In , Repository } from 'typeorm' ;
4
4
5
+ import { CheckRBAC , HandleNoun , HandleVerb } from 'src/controller' ;
5
6
import { BaseController , ErrorReplyType } from 'src/controller/BaseController' ;
6
7
import { createCompletion } from 'src/controller/CompletionController' ;
7
8
import { Controller , ControllerData , ControllerOptions } from 'src/controller/Controller' ;
@@ -55,83 +56,36 @@ export class AccountController extends BaseController<AccountControllerData> imp
55
56
this . userRepository = this . storage . getCustomRepository ( UserRepository ) ;
56
57
}
57
58
58
- public async handle ( cmd : Command ) : Promise < void > {
59
- switch ( cmd . noun ) {
60
- case NOUN_ACCOUNT :
61
- return this . handleAccount ( cmd ) ;
62
- case NOUN_GRANT :
63
- return this . handleGrant ( cmd ) ;
64
- case NOUN_SESSION :
65
- return this . handleSession ( cmd ) ;
66
- default :
67
- return this . reply ( cmd . context , `unsupported noun: ${ cmd . noun } ` ) ;
68
- }
69
- }
70
-
71
- public async handleAccount ( cmd : Command ) : Promise < void > {
72
- switch ( cmd . verb ) {
73
- case CommandVerb . Create :
74
- return this . createAccount ( cmd ) ;
75
- case CommandVerb . Delete :
76
- return this . deleteAccount ( cmd ) ;
77
- default :
78
- return this . reply ( cmd . context , `unsupported verb: ${ cmd . verb } ` ) ;
79
- }
80
- }
81
-
82
- public async handleGrant ( cmd : Command ) : Promise < void > {
83
- switch ( cmd . verb ) {
84
- case CommandVerb . Get :
85
- return this . getGrant ( cmd ) ;
86
- case CommandVerb . List :
87
- return this . listGrants ( cmd ) ;
88
- default :
89
- return this . reply ( cmd . context , `unsupported verb: ${ cmd . verb } ` ) ;
90
- }
91
- }
92
-
93
- public async handleSession ( cmd : Command ) : Promise < void > {
94
- switch ( cmd . verb ) {
95
- case CommandVerb . Create :
96
- return this . createSession ( cmd ) ;
97
- case CommandVerb . Get :
98
- return this . getSession ( cmd ) ;
99
- default :
100
- return this . reply ( cmd . context , `unsupported verb: ${ cmd . verb } ` ) ;
101
- }
102
- }
103
-
59
+ @HandleNoun ( NOUN_GRANT )
60
+ @HandleVerb ( CommandVerb . Get )
61
+ @CheckRBAC ( )
104
62
public async getGrant ( cmd : Command ) : Promise < void > {
105
- if ( ! this . checkGrants ( cmd . context , 'grant:get' ) ) {
106
- return this . errorReply ( cmd . context , ErrorReplyType . GrantMissing ) ;
107
- }
108
-
109
63
const grants = cmd . get ( 'grants' ) ;
110
64
const results = grants . map ( ( p ) => {
111
65
return `\`${ p } : ${ cmd . context . checkGrants ( [ p ] ) } \`` ;
112
66
} ) . join ( '\n' ) ;
113
67
return this . reply ( cmd . context , results ) ;
114
68
}
115
69
70
+ @HandleNoun ( NOUN_GRANT )
71
+ @HandleVerb ( CommandVerb . List )
72
+ @CheckRBAC ( )
116
73
public async listGrants ( cmd : Command ) : Promise < void > {
117
- if ( ! this . checkGrants ( cmd . context , 'grant:list' ) ) {
118
- return this . errorReply ( cmd . context , ErrorReplyType . GrantMissing ) ;
119
- }
120
-
121
74
const grants = cmd . get ( 'grants' ) ;
122
75
const results = grants . map ( ( p ) => {
123
76
return `\`${ p } : ${ cmd . context . listGrants ( [ p ] ) } \`` ;
124
77
} ) . join ( '\n' ) ;
125
78
return this . reply ( cmd . context , results ) ;
126
79
}
127
80
81
+ @HandleNoun ( NOUN_ACCOUNT )
82
+ @HandleVerb ( CommandVerb . Create )
128
83
public async createAccount ( cmd : Command ) : Promise < void > {
129
- if ( ! this . checkGrants ( cmd . context , 'account:create' ) && ! this . data . join . allow ) {
84
+ if ( ! this . data . join . allow && ! this . checkGrants ( cmd . context , 'account:create' ) ) {
130
85
return this . errorReply ( cmd . context , ErrorReplyType . GrantMissing ) ;
131
86
}
132
87
133
88
const name = cmd . getHeadOrDefault ( 'name' , cmd . context . name ) ;
134
-
135
89
if ( await this . userRepository . count ( {
136
90
name,
137
91
} ) ) {
@@ -153,63 +107,54 @@ export class AccountController extends BaseController<AccountControllerData> imp
153
107
return this . reply ( cmd . context , `user ${ name } joined, sign in token: ${ jwt } ` ) ;
154
108
}
155
109
110
+ @HandleNoun ( NOUN_ACCOUNT )
111
+ @HandleVerb ( CommandVerb . Delete )
112
+ @CheckRBAC ( )
156
113
public async deleteAccount ( cmd : Command ) : Promise < void > {
157
- if ( isNil ( cmd . context . user ) ) {
158
- return this . errorReply ( cmd . context , ErrorReplyType . SessionMissing ) ;
159
- }
160
-
161
- if ( ! this . checkGrants ( cmd . context , 'account:delete' ) ) {
162
- return this . errorReply ( cmd . context , ErrorReplyType . GrantMissing ) ;
163
- }
114
+ const user = this . getUserOrFail ( cmd . context ) ;
164
115
165
116
if ( cmd . getHeadOrDefault ( 'confirm' , 'no' ) !== 'yes' ) {
166
- const completion = createCompletion ( cmd , 'confirm' , `please confirm deleting all tokens for ${ cmd . context . user . name } ` ) ;
117
+ const completion = createCompletion ( cmd , 'confirm' , `please confirm deleting all tokens for ${ user . name } ` ) ;
167
118
await this . bot . executeCommand ( completion ) ;
168
119
return ;
169
120
}
170
121
171
122
await this . tokenRepository . delete ( {
172
- subject : cmd . context . user . id ,
123
+ subject : user . id ,
173
124
} ) ;
174
125
175
- const jwt = await this . createToken ( cmd . context . user ) ;
176
- return this . reply ( cmd . context , `revoked tokens for ${ cmd . context . user . name } , new sign in token: ${ jwt } ` ) ;
126
+ const jwt = await this . createToken ( user ) ;
127
+ return this . reply ( cmd . context , `revoked tokens for ${ user . name } , new sign in token: ${ jwt } ` ) ;
177
128
}
178
129
130
+ @HandleNoun ( NOUN_SESSION )
131
+ @HandleVerb ( CommandVerb . Create )
179
132
public async createSession ( cmd : Command ) : Promise < void > {
180
- if ( isNil ( cmd . context . source ) ) {
181
- return this . reply ( cmd . context , 'no source listener with which to create a session' ) ;
182
- }
133
+ const jwt = cmd . getHead ( 'token' ) ;
134
+ const token = Token . verify ( jwt , this . data . token . secret , {
135
+ audience : this . data . token . audience ,
136
+ issuer : this . data . token . issuer ,
137
+ } ) ;
138
+ this . logger . debug ( { token } , 'creating session from token' ) ;
183
139
184
- try {
185
- const jwt = cmd . getHead ( 'token' ) ;
186
- const token = Token . verify ( jwt , this . data . token . secret , {
187
- audience : this . data . token . audience ,
188
- issuer : this . data . token . issuer ,
189
- } ) ;
190
- this . logger . debug ( { token } , 'creating session from token' ) ;
191
-
192
- const user = await this . userRepository . findOneOrFail ( {
193
- id : token . sub ,
194
- } ) ;
195
- await this . userRepository . loadRoles ( user ) ;
196
- this . logger . debug ( { user } , 'logging in user' ) ;
197
-
198
- const session = await cmd . context . source . createSession ( cmd . context . uid , user ) ;
199
- this . logger . debug ( { session, user } , 'created session' ) ;
200
- return this . reply ( cmd . context , 'created session' ) ;
201
- } catch ( err ) {
202
- this . logger . error ( err , 'error creating session' ) ;
203
- return this . reply ( cmd . context , err . message ) ;
204
- }
140
+ const user = await this . userRepository . findOneOrFail ( {
141
+ id : token . sub ,
142
+ } ) ;
143
+ await this . userRepository . loadRoles ( user ) ;
144
+ this . logger . debug ( { user } , 'logging in user' ) ;
145
+
146
+ const source = this . getSourceOrFail ( cmd . context ) ;
147
+ const session = await source . createSession ( cmd . context . uid , user ) ;
148
+ this . logger . debug ( { session, user } , 'created session' ) ;
149
+ return this . reply ( cmd . context , 'created session' ) ;
205
150
}
206
151
152
+ @HandleNoun ( NOUN_SESSION )
153
+ @HandleVerb ( CommandVerb . Get )
154
+ @CheckRBAC ( )
207
155
public async getSession ( cmd : Command ) : Promise < void > {
208
- if ( isNil ( cmd . context . source ) ) {
209
- return this . reply ( cmd . context , 'no source listener with which to create a session' ) ;
210
- }
211
-
212
- const session = cmd . context . source . getSession ( cmd . context . uid ) ;
156
+ const source = this . getSourceOrFail ( cmd . context ) ;
157
+ const session = source . getSession ( cmd . context . uid ) ;
213
158
if ( isNil ( session ) ) {
214
159
return this . reply ( cmd . context , 'cannot get sessions unless logged in' ) ;
215
160
}
0 commit comments