@@ -13,9 +13,7 @@ import {NgbModal} from '@ng-bootstrap/ng-bootstrap';
1313import { TranslateService } from '@ngx-translate/core' ;
1414import { DomSanitizer , SafeResourceUrl } from '@angular/platform-browser' ;
1515import { IMPERSONATION_KEY } from '../auth/account.service' ;
16- import { IMPERSONATE_GRANT , IMPERSONATE_REQUEST , IMPERSONATE_ADMIN_REQUEST , IMPERSONATE_ADMIN_RESPONSE ,
17- IMPERSONATION_ADMIN_CONTEXT_KEY , ImpersonationAdminAction } from '../auth/impersonation.service' ;
18- import { isTrustedImpersonationAdminEnvelope } from '../auth/impersonation.service' ;
16+ import { IMPERSONATE_GRANT , IMPERSONATE_REQUEST } from '../auth/impersonation.service' ;
1917import { marked } from 'marked' ;
2018import DOMPurify from 'dompurify' ;
2119
@@ -53,8 +51,6 @@ export class AdminComponent implements OnInit {
5351 private impersonateNonce : string = null ;
5452 private impersonateAccount : any = null ;
5553 private impersonateListener : ( event : MessageEvent ) => void = null ;
56- private readonly impersonateActions : ImpersonationAdminAction [ ] = [ 'bind-card-by-ext-id' , 'unbind-card-by-ext-id' ,
57- 'set-default-card' , 'remove-external-code' , 'set-game-ban-state' , 'refresh-admin-context' ] ;
5854 eulaCurrent : any = null ;
5955 eulaDraftTitle = '' ;
6056 eulaDraftContent = '' ;
@@ -151,11 +147,8 @@ export class AdminComponent implements OnInit {
151147 }
152148 if ( event . data ?. type === IMPERSONATE_REQUEST ) {
153149 ( event . source as Window ) ?. postMessage ( { type : IMPERSONATE_GRANT , nonce : this . impersonateNonce ,
154- account : this . impersonateAccount , adminContext : { nonce : this . impersonateNonce ,
155- target : this . impersonateUsername , actions : this . impersonateActions } } , location . origin ) ;
156- return ;
150+ account : this . impersonateAccount } , location . origin ) ;
157151 }
158- if ( event . data ?. type === IMPERSONATE_ADMIN_REQUEST ) this . handleImpersonationAdminRequest ( event ) ;
159152 } ;
160153 window . addEventListener ( 'message' , this . impersonateListener ) ;
161154
@@ -173,7 +166,6 @@ export class AdminComponent implements OnInit {
173166 const frame = document . querySelector ( 'iframe.impersonation-frame' ) as HTMLIFrameElement ;
174167 try {
175168 frame ?. contentWindow ?. sessionStorage ?. removeItem ( IMPERSONATION_KEY ) ;
176- frame ?. contentWindow ?. sessionStorage ?. removeItem ( IMPERSONATION_ADMIN_CONTEXT_KEY ) ;
177169 } catch ( e ) {
178170 console . warn ( 'could not clear impersonated session' , e ) ;
179171 }
@@ -255,56 +247,6 @@ export class AdminComponent implements OnInit {
255247 this . modalService . open ( tpl , { centered : true , scrollable : true } ) ;
256248 }
257249
258- private handleImpersonationAdminRequest ( event : MessageEvent ) {
259- const message = event . data ;
260- const action = message ?. action as ImpersonationAdminAction ;
261- const frame = document . querySelector ( 'iframe.impersonation-frame' ) as HTMLIFrameElement ;
262- if ( ! isTrustedImpersonationAdminEnvelope ( event , frame ?. contentWindow , this . impersonateNonce ,
263- this . impersonateUsername , this . impersonateActions ) || ! this . validAdminPayload ( action , message . payload ) ) return ;
264- const target = this . impersonateUsername ;
265- const p = message . payload || { } ;
266- let request : any ;
267- switch ( action ) {
268- case 'bind-card-by-ext-id' :
269- request = this . api . post ( 'api/admin/bindCardViaExtId' , { userName : target , extId : p . extId } ) ; break ;
270- case 'unbind-card-by-ext-id' :
271- request = this . api . delete ( `api/admin/accounts/${ target } /cards/${ p . extId } ` ) ; break ;
272- case 'set-default-card' :
273- request = this . api . put ( `api/admin/accounts/${ target } /cards/${ p . extId } /default` , { } ) ; break ;
274- case 'remove-external-code' :
275- request = this . api . delete ( `api/admin/accounts/${ target } /cards/${ p . extId } /external/${ encodeURIComponent ( p . luid ) } ` ) ; break ;
276- case 'set-game-ban-state' :
277- request = this . api . put ( `api/admin/accounts/${ target } /games/${ p . game } /${ p . extId } /ban-state` , { status : p . status } ) ; break ;
278- case 'refresh-admin-context' :
279- request = this . api . get ( `api/admin/accounts/${ target } ` ) ; break ;
280- }
281- request . subscribe ( {
282- next : resp => {
283- const ok = resp ?. status ?. code === StatusCode . OK ;
284- this . replyImpersonation ( event . source as Window , message , ok , resp ?. data ,
285- ok ? undefined : ( resp ?. status ?. message || 'Admin action failed' ) ) ;
286- } ,
287- error : error => this . replyImpersonation ( event . source as Window , message , false , null , String ( error ) )
288- } ) ;
289- }
290-
291- private validAdminPayload ( action : ImpersonationAdminAction , payload : any ) {
292- if ( action === 'refresh-admin-context' ) return payload && Object . keys ( payload ) . length === 0 ;
293- if ( ! payload || ! Number . isSafeInteger ( payload . extId ) || payload . extId < 0 ) return false ;
294- if ( action === 'remove-external-code' ) return Object . keys ( payload ) . sort ( ) . join ( ',' ) === 'extId,luid' &&
295- typeof payload . luid === 'string' && payload . luid . length === 20 ;
296- if ( action === 'set-game-ban-state' ) return Object . keys ( payload ) . sort ( ) . join ( ',' ) === 'extId,game,status' &&
297- [ 'CHUSAN' , 'MAIMAI2' , 'ONGEKI' ] . includes ( payload . game ) && Number . isInteger ( payload . status ) &&
298- payload . status >= 0 && payload . status <= 2 ;
299- return Object . keys ( payload ) . length === 1 ;
300- }
301-
302- private replyImpersonation ( target : Window , request : any , ok : boolean , data ?: any , error ?: string ) {
303- if ( ! this . impersonateNonce || request . nonce !== this . impersonateNonce || request . target !== this . impersonateUsername ) return ;
304- target . postMessage ( { type : IMPERSONATE_ADMIN_RESPONSE , nonce : this . impersonateNonce ,
305- target : this . impersonateUsername , requestId : request . requestId , ok, data, error} , location . origin ) ;
306- }
307-
308250 isAdminTarget ( item : AdvancedUser ) {
309251 return ( item . user . roles || [ ] ) . some ( role => role . name === 'ROLE_ADMIN' ) ;
310252 }
0 commit comments