@@ -17,6 +17,16 @@ class SocketServer extends EventEmitter{
1717
1818 //. websocket server
1919 this . wss = new WebSocket . Server ( { noServer : true } ) ;
20+ this . permissionInstance = null ;
21+ this . authInstance = null ;
22+ }
23+
24+ setPermission ( instance ) {
25+ this . permissionInstance = instance ;
26+ }
27+
28+ setAuth ( instance ) {
29+ this . authInstance = instance
2030 }
2131
2232 handleUpgrade ( req , socket , head ) {
@@ -37,8 +47,8 @@ class SocketServer extends EventEmitter{
3747
3848 this . addClient ( ws , info . key , info ) ;
3949
40- ws . on ( 'message' , ( message ) => {
41- self . onMessage ( ws , message , info ) ;
50+ ws . on ( 'message' , async ( message ) => {
51+ await self . onMessage ( req , ws , message , info ) ;
4252 } )
4353
4454 ws . on ( 'close' , function ( ) {
@@ -121,8 +131,7 @@ class SocketServer extends EventEmitter{
121131 return params
122132 }
123133
124- onMessage ( ws , message , roomInfo ) {
125-
134+ async onMessage ( req , ws , message , roomInfo ) {
126135 try {
127136 this . recordTransfer ( 'in' , message , roomInfo . orgId )
128137
@@ -133,8 +142,21 @@ class SocketServer extends EventEmitter{
133142
134143 const requestData = JSON . parse ( message )
135144 let cloneRoomInfo = { ...roomInfo } ;
136-
145+
137146 if ( requestData . action ) {
147+ let user_id = null ;
148+ if ( this . authInstance ) {
149+ user_id = await this . authInstance . getUserId ( req ) ;
150+ }
151+ //. check permission
152+ if ( this . permissionInstance ) {
153+ let passStatus = await this . permissionInstance . check ( requestData . action , requestData . data , req , user_id )
154+ if ( ! passStatus ) {
155+ this . send ( ws , 'permissionError' , requestData . data , cloneRoomInfo . orgId , cloneRoomInfo )
156+ return ;
157+ }
158+ }
159+
138160 //. checking async status....
139161 if ( requestData . data . async == true ) {
140162 const uuid = GenerateUUID ( ) , asyncMessage = this . asyncMessages . get ( cloneRoomInfo . key ) ;
0 commit comments