web-one 0.0.10 → 0.0.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/index.js CHANGED
@@ -793,3 +793,54 @@ function isValidSlug(path) {
793
793
  return true;
794
794
  }
795
795
  exports.isValidSlug = isValidSlug;
796
+ exports.none = 0;
797
+ exports.read = 1;
798
+ exports.write = 2;
799
+ exports.approve = 4;
800
+ exports.all = 2147483647;
801
+ class PrivilegeLoader {
802
+ constructor(sql, query) {
803
+ this.sql = sql;
804
+ this.query = query;
805
+ this.getPrivilege = this.getPrivilege.bind(this);
806
+ }
807
+ getPrivilege(userId, privilegeId) {
808
+ return this.query(this.sql, [userId, privilegeId]).then((v) => {
809
+ if (!v || v.length === 0) {
810
+ return exports.none;
811
+ }
812
+ const keys = Object.keys(v[0]);
813
+ if (keys.length === 0) {
814
+ return exports.all;
815
+ }
816
+ const k = keys[0];
817
+ let permissions = 0;
818
+ let ok = false;
819
+ for (const p of v) {
820
+ const x = p[k];
821
+ if (typeof x === "number") {
822
+ permissions = permissions | x;
823
+ ok = true;
824
+ }
825
+ }
826
+ return ok ? permissions : exports.all;
827
+ });
828
+ }
829
+ }
830
+ exports.PrivilegeLoader = PrivilegeLoader;
831
+ function getPath(s, i) {
832
+ if (!i || i <= 0) {
833
+ return s;
834
+ }
835
+ let count = 0;
836
+ while (count < i) {
837
+ const k = s.lastIndexOf("/");
838
+ if (k < 0) {
839
+ return s;
840
+ }
841
+ s = s.substring(0, k);
842
+ count = count + 1;
843
+ }
844
+ return s;
845
+ }
846
+ exports.getPath = getPath;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "web-one",
3
- "version": "0.0.10",
3
+ "version": "0.0.11",
4
4
  "description": "Web utilities",
5
5
  "main": "./lib/index.js",
6
6
  "types": "./src/index.ts",
package/src/index.ts CHANGED
@@ -15,7 +15,6 @@ export function getRecordValue(v: string | string[] | undefined): string | undef
15
15
  }
16
16
  return undefined
17
17
  }
18
- // export type StringMap = Record<string, string | string[] | undefined>
19
18
  export function removePage(obj: Record<string, string | string[] | undefined>, pageKey?: string): string {
20
19
  const arr: string[] = []
21
20
  const keys = Object.keys(obj)
@@ -681,10 +680,10 @@ export function rebuildPath(items: MenuItem[], lang: string) {
681
680
  }
682
681
  }
683
682
  }
684
- interface StringMap2 {
683
+ export interface StringMap {
685
684
  [key: string]: string;
686
685
  }
687
- export function localize(items: MenuItem[], resource: StringMap2): MenuItem[] {
686
+ export function localize(items: MenuItem[], resource: StringMap): MenuItem[] {
688
687
  for (const item of items) {
689
688
  if (item.resource) {
690
689
  const text = resource[item.resource];
@@ -798,3 +797,57 @@ export function isValidSlug(path: string): boolean {
798
797
  }
799
798
  return true;
800
799
  }
800
+
801
+ export const none = 0
802
+ export const read = 1
803
+ export const write = 2
804
+ export const approve = 4
805
+ export const all = 2147483647
806
+ // tslint:disable-next-line:max-classes-per-file
807
+ export class PrivilegeLoader {
808
+ constructor(
809
+ protected sql: string,
810
+ protected query: <T>(sql: string, args?: any[]) => Promise<T[]>,
811
+ ) {
812
+ this.getPrivilege = this.getPrivilege.bind(this)
813
+ }
814
+ getPrivilege(userId: string, privilegeId: string): Promise<number> {
815
+ return this.query<any>(this.sql, [userId, privilegeId]).then((v) => {
816
+ if (!v || v.length === 0) {
817
+ return none
818
+ }
819
+ const keys = Object.keys(v[0])
820
+ if (keys.length === 0) {
821
+ return all
822
+ }
823
+ const k: string = keys[0]
824
+ let permissions = 0
825
+ let ok = false
826
+ for (const p of v) {
827
+ const x = p[k]
828
+ if (typeof x === "number") {
829
+ // tslint:disable-next-line:no-bitwise
830
+ permissions = permissions | x
831
+ ok = true
832
+ }
833
+ }
834
+ return ok ? permissions : all
835
+ })
836
+ }
837
+ }
838
+ export function getPath(s: string, i?: number): string {
839
+ if (!i || i <= 0) {
840
+ return s
841
+ }
842
+ let count = 0
843
+
844
+ while (count < i) {
845
+ const k = s.lastIndexOf("/")
846
+ if (k < 0) {
847
+ return s
848
+ }
849
+ s = s.substring(0, k)
850
+ count = count + 1
851
+ }
852
+ return s
853
+ }