-
Notifications
You must be signed in to change notification settings - Fork 1
Add admin mode #200
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add admin mode #200
Changes from 6 commits
83d717a
255da56
9e52dfc
91dba2a
efc7a68
e90083a
ef878bf
d32d7a9
7bbe895
6a74107
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,16 +1,47 @@ | ||
| import { faCalendarDays, faClock, faTicket } from '@fortawesome/free-solid-svg-icons'; | ||
| import { faCalendarDay, faCalendarDays, faClock, faTicket } from '@fortawesome/free-solid-svg-icons'; | ||
| import { MenuItem, MenuSubItem } from './menu-items'; | ||
| import { IconDefinition } from '@fortawesome/angular-fontawesome'; | ||
| import { inject } from '@angular/core'; | ||
| import { AuthService } from 'src/app/services/auth.service'; | ||
| import { AdminModeService } from 'src/app/services/adminMode.service'; | ||
|
|
||
| export class MenuBuilder { | ||
| constructor() { } | ||
| readonly #auth = inject(AuthService); | ||
| readonly #adminMode = inject(AdminModeService); | ||
|
|
||
| build(): MenuItem[] { | ||
|
|
||
| #defaultMenu: MenuItem[] = [ | ||
| this.#getMenuItem('My events', 'my-events', faCalendarDay, () => this.getSubItemsByPath('my-events')), | ||
| ].filter(o => o); | ||
|
|
||
| ticketsMenuItem: MenuItem = this.#getMenuItem('Tickets', 'tickets', faTicket, () => this.getSubItemsByPath('tickets')); | ||
| ticketGroupsMenuItem: MenuItem = this.#getMenuItem('Ticket groups', 'ticket_groups', faClock, () => this.getSubItemsByPath('ticket_groups')); | ||
| eventsMenuItem: MenuItem = this.#getMenuItem('Events', 'events', faCalendarDays, () => this.getSubItemsByPath('events')); | ||
| #adminMenu: MenuItem[] = [ | ||
| this.ticketsMenuItem, | ||
| this.ticketGroupsMenuItem, | ||
| this.eventsMenuItem, | ||
| ]; | ||
|
|
||
| build(currentUrl: string = ''): MenuItem[] { | ||
| if (this.#adminMode.status()) { | ||
| // user is admin and should have access to all things | ||
| return this.#adminMenu.filter(o => o); | ||
| } | ||
|
|
||
| // user has restricted access | ||
| let menu: MenuItem[] = []; | ||
|
|
||
| for (let i = 0; i < this.#adminMenu.length; i++) { | ||
| const menuItem = this.#adminMenu[i]; | ||
| if (currentUrl.startsWith(`/${menuItem.link}`)) { | ||
| menu.push(menuItem); | ||
| } | ||
| } | ||
|
Comment on lines
+26
to
+41
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okomentovat, ale možná to nechápu, protože nevím, jaká je architektura toho menu buildingu.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Přidal jsem komentáře, snad je to teď lepší. |
||
|
|
||
| return [ | ||
| this.#getMenuItem('Tickets', 'tickets', faTicket, () => this.getTicketsSubItems()), | ||
| this.#getMenuItem('Ticket groups', 'ticket_groups', faClock, () => this.getSubItemsByPath('ticket_groups')), | ||
| this.#getMenuItem('Events', 'events', faCalendarDays, () => this.getSubItemsByPath('events')), | ||
| ...menu, | ||
| ...this.#defaultMenu, | ||
| ].filter(o => o); | ||
| } | ||
|
|
||
|
|
@@ -19,14 +50,10 @@ export class MenuBuilder { | |
| ): MenuSubItem[] { | ||
| const result: MenuSubItem[] = []; | ||
| result.push(new MenuSubItem('List', `/${path}/list`)); | ||
| result.push(new MenuSubItem('New', `/${path}/add`)); | ||
| return result; | ||
| } | ||
|
|
||
| getTicketsSubItems(): MenuSubItem[] { | ||
| const result: MenuSubItem[] = []; | ||
| result.push(new MenuSubItem('List', '/tickets/list')); | ||
| result.push(new MenuSubItem('New', '/tickets/add')); | ||
| if (this.#adminMode.status() || this.#auth.getScopes().includes(`${path}:edit`)) { | ||
| result.push(new MenuSubItem('New', `/${path}/add`)); | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
|
|
||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hele, toto Ti celé přepíšu doma na komplu... :) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| import { inject, Injectable } from '@angular/core'; | ||
| import { StorageService } from './storage.service'; | ||
| import { LoggingService } from './logging.service'; | ||
| import { StorageKeys } from '../tokens/storage.tokens'; | ||
|
|
||
| @Injectable({ | ||
| providedIn: 'root', | ||
| }) | ||
| export class AdminModeService { | ||
| readonly #logging = inject(LoggingService); | ||
| readonly #storageService = inject(StorageService); | ||
|
|
||
| constructor() { | ||
| if (this.#storageService.get(StorageKeys.ADMIN_MODE) == null) { | ||
| this.off(true); | ||
| this.#logging.log("adminMode", `Set default initial value. Current value: ${this.status()}.`) | ||
| } | ||
| } | ||
|
|
||
| status(): boolean { | ||
| return this.#storageService.get(StorageKeys.ADMIN_MODE) == String(true); | ||
lukynmatuska marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| on(silent: boolean = false): void { | ||
| this.#storageService.set(StorageKeys.ADMIN_MODE, String(true)); | ||
| if (!silent) { | ||
| this.#logging.log("adminMode", `Force ON. Current value: ${this.status()}.`); | ||
| } | ||
| } | ||
|
|
||
| off(silent: boolean = false): void { | ||
| this.#storageService.set(StorageKeys.ADMIN_MODE, String(false)); | ||
| if (!silent) { | ||
| this.#logging.log("adminMode", `Force OFF. Current value: ${this.status()}.`); | ||
| } | ||
| } | ||
|
|
||
| toggle(): void { | ||
| this.#storageService.set(StorageKeys.ADMIN_MODE, String(!this.status())); | ||
| this.#logging.log("adminMode", `Toggle. Current value: ${this.status()}.`); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Proč ten filter?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To jsem si říkal taky, má to odjebat nevalidní objekty jako null, atd.