Browse Source

push

master
peter 6 years ago
parent
commit
a7a5a85033

+ 3
- 3
package-lock.json View File

4961
       }
4961
       }
4962
     },
4962
     },
4963
     "rpclibrary": {
4963
     "rpclibrary": {
4964
-      "version": "1.4.0",
4965
-      "resolved": "https://registry.npmjs.org/rpclibrary/-/rpclibrary-1.4.0.tgz",
4966
-      "integrity": "sha512-JUU7+HymV2ZTI+NrIVOiIr5LGFoRsPq4k/ffdUonqIpd9fjOHBQ2/PFLVXx9z94+dKxdqN8/D9ZXPwU03ovZrg==",
4964
+      "version": "1.4.1",
4965
+      "resolved": "https://registry.npmjs.org/rpclibrary/-/rpclibrary-1.4.1.tgz",
4966
+      "integrity": "sha512-Tv9zOBVT8JFMZfTJgmnX9vvUvtALCDtj/cnlYw2SxsFnj1MuH9VgPniSfThUgdwZZknjFuYVfhLaYb9Yl+eRxQ==",
4967
       "requires": {
4967
       "requires": {
4968
         "bsock": "^0.1.9",
4968
         "bsock": "^0.1.9",
4969
         "http": "0.0.0",
4969
         "http": "0.0.0",

+ 1
- 1
package.json View File

37
     "node-fetch": "^2.6.0",
37
     "node-fetch": "^2.6.0",
38
     "path": "^0.12.7",
38
     "path": "^0.12.7",
39
     "rimraf": "^3.0.0",
39
     "rimraf": "^3.0.0",
40
-    "rpclibrary": "^1.4.0",
40
+    "rpclibrary": "^1.4.1",
41
     "simple-git": "^1.124.0",
41
     "simple-git": "^1.124.0",
42
     "spawn-sync": "^2.0.0",
42
     "spawn-sync": "^2.0.0",
43
     "sqlite3": "^4.1.0",
43
     "sqlite3": "^4.1.0",

+ 13
- 6
src/backend/Admin/Admin.ts View File

146
 
146
 
147
         this.knex = Knex(conf)
147
         this.knex = Knex(conf)
148
     
148
     
149
-        await Promise.all(this.getTableDefinitions().map(async (def)=>{
150
-            const hasTable = await this.knex.schema.hasTable(def.name)
151
-            if(!hasTable){
152
-                await this.knex.schema.createTable(def.name, def.tableBuilder)
153
-            }
154
-        }))
149
+        await Promise.all(
150
+            this.getTableDefinitions()
151
+            //make unique by name
152
+            .filter((other, index, self) => index === self.findIndex(
153
+                (self) => self.name === other.name)
154
+            )
155
+            //create table if not exists
156
+            .map(async (def)=> {
157
+                const hasTable = await this.knex.schema.hasTable(def.name)
158
+                if(!hasTable)
159
+                    return await this.knex.schema.createTable(def.name, def.tableBuilder)
160
+            })
161
+        )
155
         
162
         
156
         return this.knex
163
         return this.knex
157
     }
164
     }

+ 2
- 2
src/backend/Components/Item/ItemManager.ts View File

94
         const allItems = [...T1]
94
         const allItems = [...T1]
95
         const countCache = await this.countItems()
95
         const countCache = await this.countItems()
96
         if(countCache != allItems.length){
96
         if(countCache != allItems.length){
97
-            const items:Item[] = await Promise.all(allItems.map(async (i) => this.getItem(i)))
97
+            const items:Item[] = await Promise.all(allItems.map((i) => this.getItem(i)))
98
             try{
98
             try{
99
                 await this.admin
99
                 await this.admin
100
                 .knex('items')
100
                 .knex('items')
101
                 .insert(items)
101
                 .insert(items)
102
             }catch(e){
102
             }catch(e){
103
-                console.error(e)
103
+                console.info("Skipping item insertion")
104
             }
104
             }
105
         }
105
         }
106
     }
106
     }

+ 18
- 5
src/backend/Components/Login/LoginManager.ts View File

1
 import { RPCServer } from "rpclibrary";
1
 import { RPCServer } from "rpclibrary";
2
-import { TableDefiniton, AnyRPCExporter, User, RPCPermission, _Rank, Token, Auth } from "../../Types/Types";
2
+import { TableDefiniton, AnyRPCExporter, User, RPCPermission, _Rank, Token, Auth, FrontcraftFeatureIfc, Rank } from "../../Types/Types";
3
 import { FrontworkAdmin } from "../../Admin/Admin";
3
 import { FrontworkAdmin } from "../../Admin/Admin";
4
 import { PrivilegedRPCExporter } from "../../Types/PrivilegedRPCExporter";
4
 import { PrivilegedRPCExporter } from "../../Types/PrivilegedRPCExporter";
5
 import { FrontworkComponent } from "../../Types/FrontworkComponent";
5
 import { FrontworkComponent } from "../../Types/FrontworkComponent";
65
             },{
65
             },{
66
                 name: 'rpcpermissions',
66
                 name: 'rpcpermissions',
67
                 tableBuilder: (table) => {
67
                 tableBuilder: (table) => {
68
-                    table.string("rpcName").primary().notNullable()
68
+                    table.string("name").primary().notNullable()
69
                     table.boolean("ADMIN").defaultTo(true).notNullable()
69
                     table.boolean("ADMIN").defaultTo(true).notNullable()
70
                     _Rank.forEach(r => table.boolean(r).defaultTo(false).notNullable())
70
                     _Rank.forEach(r => table.boolean(r).defaultTo(false).notNullable())
71
                 }
71
                 }
83
 
83
 
84
     async initialize(){
84
     async initialize(){
85
         await Promise.all( 
85
         await Promise.all( 
86
-            [this, ...this.exporters].flatMap(exp => exp.exportRPCFeatures().map(async (rpc) => {
86
+            [this, ...this.exporters].flatMap(exp => exp.exportRPCFeatures().map(async (feature) => {
87
             try{
87
             try{
88
-                await this.admin.knex.insert({ rpcname: rpc.name }).into('rpcpermissions')
88
+                await this.admin.knex.insert({ name: feature.name }).into('rpcpermissions')
89
             }catch(e){}
89
             }catch(e){}
90
         })))
90
         })))
91
 
91
 
102
         return await this.admin.knex.select('*').from('rpcpermissions')
102
         return await this.admin.knex.select('*').from('rpcpermissions')
103
     }
103
     }
104
 
104
 
105
+    getPermission = async (feature: keyof FrontcraftFeatureIfc, rank:Rank) : Promise<boolean> => {
106
+        const perm : RPCPermission[] = await this.admin.knex
107
+            .select(rank)
108
+            .from('rpcpermissions')
109
+            .where('name', '=', feature)
110
+
111
+        if(perm.length === 0) return false
112
+        return perm[0][rank]
113
+    }
114
+
105
     getRPCForUser = async (user:User): Promise<AnyRPCExporter[]> => {
115
     getRPCForUser = async (user:User): Promise<AnyRPCExporter[]> => {
106
-        return [...this.exportRPCFeatures(), ...this.exporters.flatMap((exp) => exp.exportRPCFeatures())]
116
+        return [
117
+            ...this.exportRPCFeatures(), 
118
+            ...this.exporters.flatMap((exp) => exp.exportRPCFeatures())
119
+        ].filter(async (feature) => await this.getPermission(<keyof FrontcraftFeatureIfc> feature.name, user.rank))
107
     }
120
     }
108
 
121
 
109
     createUser = async(user:User): Promise<User> => {
122
     createUser = async(user:User): Promise<User> => {

+ 4
- 0
src/backend/Types/Types.ts View File

88
         getSingups: (raid:Raid) => Promise<Signup[]>
88
         getSingups: (raid:Raid) => Promise<Signup[]>
89
         sign: (user:User, raid:Raid, attending:boolean) => Promise<any>
89
         sign: (user:User, raid:Raid, attending:boolean) => Promise<any>
90
     }
90
     }
91
+}
92
+
93
+export type SomeOf<T> = {
94
+    [K in keyof T]? : T[K]
91
 }
95
 }

+ 3
- 3
src/frontend/package-lock.json View File

14269
       "integrity": "sha512-ZYzRkETgBrdEGzL5JSKimvjI2CX7ioyZCkX2BpcfyjqI+079W0wHAyj5W4rIZMcDSOHgLZtgz1IdDi/vU77KEQ=="
14269
       "integrity": "sha512-ZYzRkETgBrdEGzL5JSKimvjI2CX7ioyZCkX2BpcfyjqI+079W0wHAyj5W4rIZMcDSOHgLZtgz1IdDi/vU77KEQ=="
14270
     },
14270
     },
14271
     "rpclibrary": {
14271
     "rpclibrary": {
14272
-      "version": "1.4.0",
14273
-      "resolved": "https://registry.npmjs.org/rpclibrary/-/rpclibrary-1.4.0.tgz",
14274
-      "integrity": "sha512-JUU7+HymV2ZTI+NrIVOiIr5LGFoRsPq4k/ffdUonqIpd9fjOHBQ2/PFLVXx9z94+dKxdqN8/D9ZXPwU03ovZrg==",
14272
+      "version": "1.4.2",
14273
+      "resolved": "https://registry.npmjs.org/rpclibrary/-/rpclibrary-1.4.2.tgz",
14274
+      "integrity": "sha512-IFigD+65a9MM+1AgEH5nurUzCyMg9hawqtnOA67/PtTGP5GsnWxaFESKl03k8L/PQ8ptZkWcub/63bfxkrwyMw==",
14275
       "requires": {
14275
       "requires": {
14276
         "bsock": "^0.1.9",
14276
         "bsock": "^0.1.9",
14277
         "http": "0.0.0",
14277
         "http": "0.0.0",

+ 1
- 1
src/frontend/package.json View File

69
     "normalize.css": "6.0.0",
69
     "normalize.css": "6.0.0",
70
     "pace-js": "1.0.2",
70
     "pace-js": "1.0.2",
71
     "roboto-fontface": "0.8.0",
71
     "roboto-fontface": "0.8.0",
72
-    "rpclibrary": "^1.4.0",
72
+    "rpclibrary": "^1.4.2",
73
     "rxjs": "6.5.2",
73
     "rxjs": "6.5.2",
74
     "rxjs-compat": "6.3.0",
74
     "rxjs-compat": "6.3.0",
75
     "socicon": "3.0.5",
75
     "socicon": "3.0.5",

+ 3
- 0
src/frontend/src/app/app.component.ts View File

19
 
19
 
20
   ngOnInit(): void {
20
   ngOnInit(): void {
21
     this.analytics.trackPageViews();
21
     this.analytics.trackPageViews();
22
+    this.loginSvc.getFeature('createUser').then( (f) => {
23
+     
24
+    })
22
     window['s'] = this.loginSvc
25
     window['s'] = this.loginSvc
23
   }
26
   }
24
 }
27
 }

+ 31
- 11
src/frontend/src/app/frontcraft/login-api.ts View File

1
 import { Injectable } from "@angular/core";
1
 import { Injectable } from "@angular/core";
2
 import {RPCSocket} from 'rpclibrary/js/src/Frontend'
2
 import {RPCSocket} from 'rpclibrary/js/src/Frontend'
3
 
3
 
4
-import { Token, Auth, User, _Rank, _Class, FrontcraftFeatureIfc, LoginManagerIfc, } from '../../../../backend/Types/Types'
4
+import { Token, Auth, User, _Class, FrontcraftFeatureIfc, LoginManagerIfc, SomeOf, } from '../../../../backend/Types/Types'
5
 import { CookieService } from 'ngx-cookie-service';
5
 import { CookieService } from 'ngx-cookie-service';
6
 
6
 
7
 @Injectable()
7
 @Injectable()
8
 export class LoginApiService{
8
 export class LoginApiService{
9
     private socket:RPCSocket & LoginManagerIfc;
9
     private socket:RPCSocket & LoginManagerIfc;
10
     private auth:Auth
10
     private auth:Auth
11
-    private privSocket: RPCSocket & FrontcraftFeatureIfc
11
+    private privSocket: RPCSocket & SomeOf<FrontcraftFeatureIfc>
12
 
12
 
13
     constructor(
13
     constructor(
14
         private cookieSvc : CookieService
14
         private cookieSvc : CookieService
16
 
16
 
17
     getUnprivilegedSocket = () : RPCSocket & LoginManagerIfc => this.socket
17
     getUnprivilegedSocket = () : RPCSocket & LoginManagerIfc => this.socket
18
 
18
 
19
-    private getPrivilegedSocket = async (auth:Auth) : Promise<RPCSocket & FrontcraftFeatureIfc> => {
19
+    private getPrivilegedSocket = async (auth:Auth) : Promise<RPCSocket & SomeOf<FrontcraftFeatureIfc>> => {
20
         if(this.privSocket) return this.privSocket
20
         if(this.privSocket) return this.privSocket
21
+        if(auth.user.rank === 'Guest'){
22
+            return await RPCSocket.makeSocket<SomeOf<FrontcraftFeatureIfc>>(
23
+                20001,
24
+                window.location.hostname
25
+            )
26
+        }
21
         try{
27
         try{
22
-            const sock: RPCSocket & FrontcraftFeatureIfc = await new RPCSocket(auth.port, window.location.hostname).connect(auth.token.value)
28
+            const sock = await RPCSocket.makeSocket<SomeOf<FrontcraftFeatureIfc>>(
29
+                auth.port, 
30
+                window.location.hostname, 
31
+                auth.token.value
32
+            )
33
+
23
             //login success
34
             //login success
24
             this.auth = auth
35
             this.auth = auth
25
             this.privSocket = sock
36
             this.privSocket = sock
29
             //login failed
40
             //login failed
30
             throw new Error('login failed')
41
             throw new Error('login failed')
31
         }
42
         }
32
-    } 
43
+    }
44
+
45
+    getFeature = async <K extends keyof FrontcraftFeatureIfc>(feature : K) : Promise<void | FrontcraftFeatureIfc[K]> => {
46
+        const sock = await this.getPrivilegedSocket(this.auth)
47
+        if(sock[feature]) return <FrontcraftFeatureIfc[K]> sock[feature]
48
+    }
33
 
49
 
34
     getCurrentUser = () : User => this.auth 
50
     getCurrentUser = () : User => this.auth 
35
                                 ? this.auth.user 
51
                                 ? this.auth.user 
40
                                     rank: 'Guest'
56
                                     rank: 'Guest'
41
                                 }
57
                                 }
42
 
58
 
43
-    authenticate = async (token : Token | string) : Promise<RPCSocket & FrontcraftFeatureIfc> => {
59
+    authenticate = async (token : Token | string) : Promise<RPCSocket & SomeOf<FrontcraftFeatureIfc>> => {
44
         const auth = await this.socket.Authenticator.authenticate(token)
60
         const auth = await this.socket.Authenticator.authenticate(token)
45
         return await this.getPrivilegedSocket(auth)
61
         return await this.getPrivilegedSocket(auth)
46
     }
62
     }
47
 
63
 
48
-    login = async (username, password) => {
64
+    login = async (username: string, password: string) => {
49
         const buf = str2arraybuf(password)
65
         const buf = str2arraybuf(password)
50
         const pwHash = await crypto.subtle.digest('SHA-256', buf);
66
         const pwHash = await crypto.subtle.digest('SHA-256', buf);
51
         const token = await this.socket.Authenticator.login(username, buf2hex(pwHash))
67
         const token = await this.socket.Authenticator.login(username, buf2hex(pwHash))
69
     }
85
     }
70
 
86
 
71
     initialize = async () : Promise<RPCSocket> => {
87
     initialize = async () : Promise<RPCSocket> => {
72
-        const sock : RPCSocket & LoginManagerIfc = await new RPCSocket(20000, window.location.hostname).connect()
88
+        const sock = await RPCSocket.makeSocket<LoginManagerIfc>(20000, window.location.hostname)
73
         this.socket = sock
89
         this.socket = sock
74
 
90
 
75
         const cookie = this.getCookie()
91
         const cookie = this.getCookie()
76
         if(cookie) {
92
         if(cookie) {
77
-            return await this.authenticate(cookie)
93
+            try{
94
+                return await this.authenticate(cookie)
95
+            }catch(e){
96
+                this.logout()
97
+            }
78
         }
98
         }
79
 
99
 
80
         return sock
100
         return sock
89
     return Array.prototype.map.call(new Uint8Array(buffer), x => ('00' + x.toString(16)).slice(-2)).join('');
109
     return Array.prototype.map.call(new Uint8Array(buffer), x => ('00' + x.toString(16)).slice(-2)).join('');
90
 }
110
 }
91
 
111
 
92
-
93
-export function initializeLoginSvc(svc: LoginApiService): () => Promise<RPCSocket> {
112
+//angular depenency manager requires this
113
+export function initializeLoginSvc(svc: LoginApiService): () => Promise<any> {
94
     return svc.initialize
114
     return svc.initialize
95
 }
115
 }

Loading…
Cancel
Save