@tblabs/storage 5.2.5 → 5.2.7

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/Dataset.d.ts CHANGED
@@ -2,6 +2,7 @@ import { OnlineStorage } from "./OnlineStorage";
2
2
  import { IConverter } from "./IConverter";
3
3
  import { IRepo } from "./IRepo";
4
4
  import { dir } from "./Types/dir";
5
+ import { IStorageUserPrivileges } from "./IStorageUserPrivileges";
5
6
  export declare class Dataset<T extends object> implements IRepo<T> {
6
7
  private _db;
7
8
  private _converter;
@@ -9,6 +10,7 @@ export declare class Dataset<T extends object> implements IRepo<T> {
9
10
  get Name(): string;
10
11
  constructor(_db: OnlineStorage, _converter: IConverter<any, T>, dataset: dir);
11
12
  private get DatasetPath();
13
+ CanUserDo(action: keyof IStorageUserPrivileges): boolean;
12
14
  GetOne(id: string): Promise<T>;
13
15
  FindOne(id: string): Promise<T | null>;
14
16
  GetAll(): Promise<T[]>;
package/Dataset.js CHANGED
@@ -20,6 +20,21 @@ export class Dataset {
20
20
  get DatasetPath() {
21
21
  return this._db.DatabaseDir + "/Datasets/" + this.dataset;
22
22
  }
23
+ CanUserDo(action) {
24
+ // console.log("ACTION", action);
25
+ // console.log("PRIVILEGES", this._db.User.Privileges);
26
+ if (!this._db.User.Privileges) {
27
+ // console.warn("User not logged in (even if Anonymous)");
28
+ return false;
29
+ }
30
+ if (typeof this._db.User.Privileges[action] === "object" && this._db.User.Privileges[action]) {
31
+ return this._db.User.Privileges[action].Datasets == "all" || this._db.User.Privileges[action].Datasets.includes(this.dataset);
32
+ }
33
+ if (typeof this._db.User.Privileges[action] === "boolean" && this._db.User.Privileges[action]) {
34
+ return this._db.User.Privileges[action];
35
+ }
36
+ return false;
37
+ }
23
38
  async GetOne(id) {
24
39
  this._db.Log(`Fetching "${id}"...`);
25
40
  const queryResult = await this._db.Send(new GetQuery(this._db.DatabaseDir, this.dataset, id));
package/IStorageUser.d.ts CHANGED
@@ -1,50 +1,15 @@
1
+ import { IStorageUserPrivileges } from "./IStorageUserPrivileges";
1
2
  import { ISender } from "./MessageBus/ISender";
2
- export type IEntitiesSet = string[] | "all" | "none";
3
- export type ICommandPrivilege = {
4
- Datasets?: IEntitiesSet;
5
- Files?: IEntitiesSet;
6
- };
7
- export interface IStorageUserPrivileges {
8
- Add: {
9
- Datasets: IEntitiesSet;
10
- };
11
- Update: {
12
- Files: IEntitiesSet;
13
- };
14
- Delete: {
15
- Files: IEntitiesSet;
16
- };
17
- List: {
18
- Datasets: IEntitiesSet;
19
- };
20
- Get: {
21
- Files: IEntitiesSet;
22
- };
23
- Drop: {
24
- Datasets: IEntitiesSet;
25
- };
26
- Move: {
27
- Datasets: IEntitiesSet;
28
- Files: IEntitiesSet;
29
- };
30
- Create: {
31
- Datasets: IEntitiesSet;
32
- };
33
- Exists: {
34
- Datasets: IEntitiesSet;
35
- };
36
- Remove: {
37
- Datasets: IEntitiesSet;
38
- };
39
- Register: boolean;
40
- }
41
3
  export interface IStorageUser extends ISender {
42
4
  Id: string;
43
5
  Database: string;
44
6
  Name: string;
45
7
  Role: string;
46
8
  Icon: string;
9
+ Login: string;
47
10
  Password: string;
11
+ Email: string;
48
12
  Privileges: IStorageUserPrivileges;
49
13
  Tags: string[];
14
+ CreatedAt: Date;
50
15
  }
package/IStorageUser.js CHANGED
@@ -1,2 +1 @@
1
- ;
2
1
  export {};
@@ -0,0 +1,38 @@
1
+ export type IEntitiesSet = string[] | "all" | "none";
2
+ export interface IStorageUserPrivileges {
3
+ Add: {
4
+ Datasets: IEntitiesSet;
5
+ };
6
+ Update: {
7
+ Datasets: IEntitiesSet;
8
+ Files: IEntitiesSet;
9
+ };
10
+ Delete: {
11
+ Datasets: IEntitiesSet;
12
+ Files: IEntitiesSet;
13
+ };
14
+ List: {
15
+ Datasets: IEntitiesSet;
16
+ };
17
+ Get: {
18
+ Datasets: IEntitiesSet;
19
+ Files: IEntitiesSet;
20
+ };
21
+ Drop: {
22
+ Datasets: IEntitiesSet;
23
+ };
24
+ Move: {
25
+ Datasets: IEntitiesSet;
26
+ Files: IEntitiesSet;
27
+ };
28
+ Create: {
29
+ Datasets: IEntitiesSet;
30
+ };
31
+ Exists: {
32
+ Datasets: IEntitiesSet;
33
+ };
34
+ Remove: {
35
+ Datasets: IEntitiesSet;
36
+ };
37
+ Register: boolean;
38
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -1,6 +1,6 @@
1
- import { IStorageUserPrivileges } from "../IStorageUser";
1
+ import { IStorageUserPrivileges } from "../IStorageUserPrivileges";
2
2
  import { Message } from "../MessageBus/Message";
3
3
  import { dir } from "../Types/dir";
4
4
  export declare class RegisterCommand extends Message {
5
- constructor(Database: dir, Id: string, Role: string, Name: string, Password: string, Privileges: IStorageUserPrivileges, Icon?: string, Tags?: string[]);
5
+ constructor(Database: dir, Id: string, Role: string, Name: string, Login: string, Password: string, Email: string, Privileges: IStorageUserPrivileges, Icon?: string, Tags?: string[], CreatedAt?: Date);
6
6
  }
@@ -1,6 +1,6 @@
1
1
  import { Message } from "../MessageBus/Message";
2
2
  export class RegisterCommand extends Message {
3
- constructor(Database, Id, Role, Name, Password, Privileges, Icon, Tags) {
4
- super("Register", { Database, Id, Role, Icon, Name, Password, Privileges, Tags });
3
+ constructor(Database, Id, Role, Name, Login, Password, Email, Privileges, Icon, Tags, CreatedAt) {
4
+ super("Register", { Database, Id, Role, Name, Login, Password, Email, Privileges, Icon, Tags, CreatedAt });
5
5
  }
6
6
  }
@@ -4,7 +4,7 @@ import { ISender } from "./MessageBus/ISender";
4
4
  import { MessageBus } from "./MessageBus/MessageBus";
5
5
  import { MessageBusResponse } from "./MessageBus/MessageBusResponse";
6
6
  import { Dataset } from "./Dataset";
7
- import { IStorageUser, IStorageUserPrivileges } from "./IStorageUser";
7
+ import { IStorageUser } from "./IStorageUser";
8
8
  export declare class OnlineStorage {
9
9
  private database;
10
10
  private ANONYMOUS_USER;
@@ -15,13 +15,14 @@ export declare class OnlineStorage {
15
15
  private readonly USER_LOCAL_STORAGE_KEY;
16
16
  get DatabaseDir(): dir;
17
17
  constructor(server: url, database: dir, sender?: ISender, bus?: MessageBus);
18
+ private WasAnonymousUserEverLoaded;
19
+ InitOnce(): Promise<void>;
18
20
  private IsBrowserEnvironment;
19
21
  private LoadUserFromLocalStorage;
20
22
  get User(): IStorageUser;
21
23
  Login(password: string): Promise<IStorageUser>;
22
24
  private SaveUserInLocalStorage;
23
- OnUserChange(handler: (user: IStorageUser, isAuthorized: boolean) => void): this;
24
- Register(id: string, role: string, name: string, password: string, privileges: IStorageUserPrivileges, icon?: string, tags?: string[]): Promise<IStorageUser>;
25
+ OnUserChange(handler: (user: IStorageUser, isAuthorized: boolean, appStart: boolean) => void): this;
25
26
  Logout(): this;
26
27
  private CallOnUserChanged;
27
28
  get IsAuthorized(): boolean;
package/OnlineStorage.js CHANGED
@@ -2,7 +2,6 @@ import { MessageBus } from "./MessageBus/MessageBus";
2
2
  import { CreateCommand, ExistsCommand, RemoveCommand } from "./Messages/AddDirCommand";
3
3
  import { MoveCommand } from "./Messages/MoveCommand";
4
4
  import { LoginQuery } from "./Messages/LoginQuery";
5
- import { RegisterCommand } from "./Messages/RegisterCommand";
6
5
  export class OnlineStorage {
7
6
  database;
8
7
  ANONYMOUS_USER = { Id: "Anonymous" };
@@ -18,18 +17,30 @@ export class OnlineStorage {
18
17
  this.database = database;
19
18
  this._bus = bus || new MessageBus(server, sender);
20
19
  this.USER_LOCAL_STORAGE_KEY = "OnlineStorage:User:" + this.database;
21
- this.LoadUserFromLocalStorage();
20
+ this.LoadUserFromLocalStorage(true);
21
+ }
22
+ WasAnonymousUserEverLoaded() {
23
+ // console.log( "aaaaAAA", this.user.Privileges)
24
+ return (this.User.Id === this.ANONYMOUS_USER.Id && this.user.Privileges !== undefined);
25
+ }
26
+ async InitOnce() {
27
+ // console.log("INIT", this.WasAnonymousUserEverLoaded());
28
+ if (this.WasAnonymousUserEverLoaded()) {
29
+ // console.log("INIT RETURN WasAnonymousUserEverLoaded()=true");
30
+ return;
31
+ }
32
+ this.user = await this.Login("Anonymous"); // TODO: Tutaj można my zrobić dedykowaną metode do pobierania ustawień użytkownika Anonymous
22
33
  }
23
34
  IsBrowserEnvironment() {
24
35
  return typeof window !== "undefined" && typeof window.localStorage !== "undefined";
25
36
  }
26
- LoadUserFromLocalStorage() {
37
+ LoadUserFromLocalStorage(appStart) {
27
38
  if (this.IsBrowserEnvironment()) {
28
39
  const user = window.localStorage.getItem(this.USER_LOCAL_STORAGE_KEY);
29
40
  if (user) {
30
41
  this.user = JSON.parse(user);
31
42
  this.SetUser(this.user || this.ANONYMOUS_USER);
32
- this.CallOnUserChanged();
43
+ this.CallOnUserChanged(appStart);
33
44
  }
34
45
  }
35
46
  }
@@ -37,6 +48,7 @@ export class OnlineStorage {
37
48
  return this.user;
38
49
  }
39
50
  async Login(password) {
51
+ // console.log("LOGIN", password);
40
52
  const result = await this._bus.SendMessage(new LoginQuery(this.DatabaseDir, password));
41
53
  if (!result.IsSuccess) {
42
54
  throw new Error(`Could not login: ${result.ErrorMessage}`);
@@ -44,7 +56,8 @@ export class OnlineStorage {
44
56
  this.user = result.Result || this.ANONYMOUS_USER;
45
57
  this.SetUser(this.user);
46
58
  this.SaveUserInLocalStorage();
47
- this.CallOnUserChanged();
59
+ this.CallOnUserChanged(false);
60
+ // console.log("USER LOGIN", this.user);
48
61
  return this.user;
49
62
  }
50
63
  SaveUserInLocalStorage() {
@@ -54,25 +67,36 @@ export class OnlineStorage {
54
67
  }
55
68
  OnUserChange(handler) {
56
69
  this.onUserChange.push(handler);
57
- handler(this.user, this.IsAuthorized);
70
+ handler(this.user, this.IsAuthorized, true);
58
71
  return this;
59
72
  }
60
- async Register(id, role, name, password, privileges, icon, tags) {
61
- const result = await this._bus.SendMessage(new RegisterCommand(this.DatabaseDir, id, role, name, password, privileges, icon, tags));
62
- if (!result.IsSuccess) {
63
- throw new Error(`Could not register: ${result.ErrorMessage}`);
64
- }
65
- const user = result.Result;
66
- return user;
67
- }
73
+ //
74
+ // UWAGA!!!!!!!!!! TAK NIE MOŻNA REJESTROWAĆ Privileges!!!!!! TO MUSI SIĘ ODBYWAĆ PRZEZ INNĄ METODE ALBO INNY MECHANIZM!!!!!!!
75
+ // Użytkownik nie może sam sobie nadawać uprawnień xddd
76
+ //
77
+ //
78
+ // public async Register(id: string, role: string, name: string,
79
+ // login: string, password: string, email: string,
80
+ // privileges: IStorageUserPrivileges,
81
+ // icon?: string, tags?: string[]): Promise<IStorageUser>
82
+ // {
83
+ // const createdAt = new Date();
84
+ // const result = await this._bus.SendMessage(new RegisterCommand(this.DatabaseDir, id, role, name, login, password, email, privileges, icon, tags, createdAt));
85
+ // if (!result.IsSuccess)
86
+ // {
87
+ // throw new Error(`Could not register: ${result.ErrorMessage}`);
88
+ // }
89
+ // const user = result.Result as IStorageUser;
90
+ // return user;
91
+ // }
68
92
  Logout() {
69
93
  this.SetUser(this.ANONYMOUS_USER);
70
94
  this.SaveUserInLocalStorage();
71
- this.CallOnUserChanged();
95
+ this.CallOnUserChanged(false);
72
96
  return this;
73
97
  }
74
- CallOnUserChanged() {
75
- this.onUserChange.forEach(handler => handler(this.user, this.IsAuthorized));
98
+ CallOnUserChanged(appStart) {
99
+ this.onUserChange.forEach(handler => handler(this.user, this.IsAuthorized, appStart));
76
100
  }
77
101
  get IsAuthorized() {
78
102
  return this.user.Id !== this.ANONYMOUS_USER.Id;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tblabs/storage",
3
- "version": "5.2.5",
3
+ "version": "5.2.7",
4
4
  "description": "online storage module with auth",
5
5
  "license": "beerware",
6
6
  "main": "index.js",
Binary file
Binary file