This commit is contained in:
peter
2019-11-09 11:53:36 +01:00
parent 92824d67a5
commit a7a5a85033
10 changed files with 79 additions and 32 deletions
+13 -6
View File
@@ -146,12 +146,19 @@ implements TableDefinitionExporter {
this.knex = Knex(conf)
await Promise.all(this.getTableDefinitions().map(async (def)=>{
const hasTable = await this.knex.schema.hasTable(def.name)
if(!hasTable){
await this.knex.schema.createTable(def.name, def.tableBuilder)
}
}))
await Promise.all(
this.getTableDefinitions()
//make unique by name
.filter((other, index, self) => index === self.findIndex(
(self) => self.name === other.name)
)
//create table if not exists
.map(async (def)=> {
const hasTable = await this.knex.schema.hasTable(def.name)
if(!hasTable)
return await this.knex.schema.createTable(def.name, def.tableBuilder)
})
)
return this.knex
}
+2 -2
View File
@@ -94,13 +94,13 @@ implements FrontworkComponent<ItemManagerFeatureIfc>, TableDefinitionExporter{
const allItems = [...T1]
const countCache = await this.countItems()
if(countCache != allItems.length){
const items:Item[] = await Promise.all(allItems.map(async (i) => this.getItem(i)))
const items:Item[] = await Promise.all(allItems.map((i) => this.getItem(i)))
try{
await this.admin
.knex('items')
.insert(items)
}catch(e){
console.error(e)
console.info("Skipping item insertion")
}
}
}
+18 -5
View File
@@ -1,5 +1,5 @@
import { RPCServer } from "rpclibrary";
import { TableDefiniton, AnyRPCExporter, User, RPCPermission, _Rank, Token, Auth } from "../../Types/Types";
import { TableDefiniton, AnyRPCExporter, User, RPCPermission, _Rank, Token, Auth, FrontcraftFeatureIfc, Rank } from "../../Types/Types";
import { FrontworkAdmin } from "../../Admin/Admin";
import { PrivilegedRPCExporter } from "../../Types/PrivilegedRPCExporter";
import { FrontworkComponent } from "../../Types/FrontworkComponent";
@@ -65,7 +65,7 @@ implements FrontworkComponent{
},{
name: 'rpcpermissions',
tableBuilder: (table) => {
table.string("rpcName").primary().notNullable()
table.string("name").primary().notNullable()
table.boolean("ADMIN").defaultTo(true).notNullable()
_Rank.forEach(r => table.boolean(r).defaultTo(false).notNullable())
}
@@ -83,9 +83,9 @@ implements FrontworkComponent{
async initialize(){
await Promise.all(
[this, ...this.exporters].flatMap(exp => exp.exportRPCFeatures().map(async (rpc) => {
[this, ...this.exporters].flatMap(exp => exp.exportRPCFeatures().map(async (feature) => {
try{
await this.admin.knex.insert({ rpcname: rpc.name }).into('rpcpermissions')
await this.admin.knex.insert({ name: feature.name }).into('rpcpermissions')
}catch(e){}
})))
@@ -102,8 +102,21 @@ implements FrontworkComponent{
return await this.admin.knex.select('*').from('rpcpermissions')
}
getPermission = async (feature: keyof FrontcraftFeatureIfc, rank:Rank) : Promise<boolean> => {
const perm : RPCPermission[] = await this.admin.knex
.select(rank)
.from('rpcpermissions')
.where('name', '=', feature)
if(perm.length === 0) return false
return perm[0][rank]
}
getRPCForUser = async (user:User): Promise<AnyRPCExporter[]> => {
return [...this.exportRPCFeatures(), ...this.exporters.flatMap((exp) => exp.exportRPCFeatures())]
return [
...this.exportRPCFeatures(),
...this.exporters.flatMap((exp) => exp.exportRPCFeatures())
].filter(async (feature) => await this.getPermission(<keyof FrontcraftFeatureIfc> feature.name, user.rank))
}
createUser = async(user:User): Promise<User> => {
+4
View File
@@ -88,4 +88,8 @@ export type RaidManagerFeatureIfc = {
getSingups: (raid:Raid) => Promise<Signup[]>
sign: (user:User, raid:Raid, attending:boolean) => Promise<any>
}
}
export type SomeOf<T> = {
[K in keyof T]? : T[K]
}