From 7d67b08ce6795e82cb13db959e5e8b4b402df0e3 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Sat, 18 Feb 2023 12:14:04 +0100 Subject: [PATCH] Added leftServer handling to members list #130 --- kdb-bot/src/bot_data/model/user.py | 11 +++++ .../src/bot_graphql/filter/server_filter.py | 4 +- kdb-bot/src/bot_graphql/filter/user_filter.py | 2 +- kdb-bot/src/bot_graphql/queries/user_query.py | 5 +- kdb-web/src/app/models/auth/auth-user.dto.ts | 2 +- .../server/members/members.component.html | 23 +++++++++ .../view/server/members/members.component.ts | 48 +++++++++++-------- .../view/server/profile/profile.component.ts | 26 ++++++++-- kdb-web/src/app/services/data/data.service.ts | 12 ----- .../app/services/sidebar/sidebar.service.ts | 2 +- kdb-web/src/assets/i18n/de.json | 5 +- 11 files changed, 97 insertions(+), 43 deletions(-) diff --git a/kdb-bot/src/bot_data/model/user.py b/kdb-bot/src/bot_data/model/user.py index 151b4b40..f115df89 100644 --- a/kdb-bot/src/bot_data/model/user.py +++ b/kdb-bot/src/bot_data/model/user.py @@ -86,6 +86,17 @@ class User(TableABC): def server(self) -> Optional[Server]: return self._server + @property + @ServiceProviderABC.inject + def left_server( + self, + services: ServiceProviderABC, + ) -> bool: + from bot_data.abc.user_joined_server_repository_abc import UserJoinedServerRepositoryABC + + ujs: UserJoinedServerRepositoryABC = services.get_service(UserJoinedServerRepositoryABC) + return ujs.find_active_user_joined_server_by_user_id(self.id) is None + @staticmethod def get_select_all_string() -> str: return str( diff --git a/kdb-bot/src/bot_graphql/filter/server_filter.py b/kdb-bot/src/bot_graphql/filter/server_filter.py index 6260f3a9..e090323e 100644 --- a/kdb-bot/src/bot_graphql/filter/server_filter.py +++ b/kdb-bot/src/bot_graphql/filter/server_filter.py @@ -31,7 +31,9 @@ class ServerFilter(FilterABC): query = query.where(lambda x: x.id == self._id) if self._discord_id is not None: - query = query.where(lambda x: x.discord_id == self._discord_id) + query = query.where( + lambda x: x.discord_id == self._discord_id or str(self._discord_id) in str(x.discord_id) + ) if self._name is not None: diff --git a/kdb-bot/src/bot_graphql/filter/user_filter.py b/kdb-bot/src/bot_graphql/filter/user_filter.py index ebc26253..c8198542 100644 --- a/kdb-bot/src/bot_graphql/filter/user_filter.py +++ b/kdb-bot/src/bot_graphql/filter/user_filter.py @@ -73,7 +73,7 @@ class UserFilter(FilterABC): def filter(self, query: List[User]) -> List[User]: if self._id is not None: - query = query.where(lambda x: x.id == self._id or str(self._id) in str(x.id)) + query = query.where(lambda x: x.id == self._id) if self._discord_id is not None: query = query.where( diff --git a/kdb-bot/src/bot_graphql/queries/user_query.py b/kdb-bot/src/bot_graphql/queries/user_query.py index e609e81d..857d668d 100644 --- a/kdb-bot/src/bot_graphql/queries/user_query.py +++ b/kdb-bot/src/bot_graphql/queries/user_query.py @@ -91,5 +91,6 @@ class UserQuery(DataQueryABC): def resolve_server(user: User, *_): return user.server - def resolve_left_server(self, user: User, *_): - return self._ujs.find_active_user_joined_server_by_user_id(user.id) is None + @staticmethod + def resolve_left_server(user: User, *_): + return user.left_server diff --git a/kdb-web/src/app/models/auth/auth-user.dto.ts b/kdb-web/src/app/models/auth/auth-user.dto.ts index 4d7b7f6a..fc49619d 100644 --- a/kdb-web/src/app/models/auth/auth-user.dto.ts +++ b/kdb-web/src/app/models/auth/auth-user.dto.ts @@ -24,5 +24,5 @@ export interface UserDTO { modifiedAt: string; isTechnician: boolean; isAdmin: boolean; - IsModerator: boolean; + isModerator: boolean; } diff --git a/kdb-web/src/app/modules/view/server/members/members.component.html b/kdb-web/src/app/modules/view/server/members/members.component.html index ce92f0b2..1b31fc01 100644 --- a/kdb-web/src/app/modules/view/server/members/members.component.html +++ b/kdb-web/src/app/modules/view/server/members/members.component.html @@ -61,6 +61,13 @@ + +
+
{{'view.server.members.headers.left_server' | translate}}
+ +
+ +
{{'view.server.members.headers.level' | translate}}
@@ -92,6 +99,11 @@ + +
+ +
+
@@ -153,6 +165,16 @@ + + + + {{!member.leftServer | bool}} + + + {{!member.leftServer | bool}} + + + @@ -167,6 +189,7 @@
+ diff --git a/kdb-web/src/app/modules/view/server/members/members.component.ts b/kdb-web/src/app/modules/view/server/members/members.component.ts index 99f4dbef..bc98e1ee 100644 --- a/kdb-web/src/app/modules/view/server/members/members.component.ts +++ b/kdb-web/src/app/modules/view/server/members/members.component.ts @@ -29,6 +29,10 @@ export class MembersComponent { members!: User[]; // levelsFilter!: MenuItem[]; levels!: MenuItem[]; + leftServerOptions = [ + {label: this.translate.instant('common.bool_as_string.true'), value: false}, + {label: this.translate.instant('common.bool_as_string.false'), value: true}, + ]; loading = true; clonedUsers: { [s: string]: User; } = {}; @@ -60,12 +64,11 @@ export class MembersComponent { id: FormControl, discordId: FormControl, name: FormControl, + leftServer: FormControl, level: FormControl }>; - filter: UserFilter = { - leftServer: false - }; + filter: UserFilter = {}; page: Page = { pageSize: undefined, pageIndex: undefined @@ -86,7 +89,7 @@ export class MembersComponent { private translate: TranslateService, private data: DataService, private sidebar: SidebarService, - private route: ActivatedRoute, + private route: ActivatedRoute ) { } @@ -110,12 +113,26 @@ export class MembersComponent { this.loadNextPage(); } + loadNextPage() { + this.loading = true; + this.data.query(Queries.usersQuery, { + filter: this.filter, page: this.page, sort: this.sort + } + ).subscribe(data => { + this.totalRecords = data.userCount; + this.members = data.users; + this.spinner.hideSpinner(); + this.loading = false; + }); + } + setFilterForm() { this.filterForm = this.fb.group({ id: new FormControl(null), discordId: new FormControl(null), name: [""], - level: new FormControl(null) + leftServer: new FormControl(null), + level: new FormControl(null), }); this.filterForm.valueChanges.pipe( @@ -139,6 +156,12 @@ export class MembersComponent { this.filter.name = undefined; } + if (changes.leftServer !== undefined && changes.leftServer !== null) { + this.filter.leftServer = changes.leftServer; + } else { + this.filter.leftServer = undefined; + } + if (changes.level) { this.filter.level = { id: changes.level @@ -157,25 +180,12 @@ export class MembersComponent { }); } - loadNextPage() { - this.loading = true; - this.data.query(Queries.usersQuery, { - filter: this.filter, page: this.page, sort: this.sort - } - ).subscribe(data => { - this.totalRecords = data.userCount; - this.members = data.users; - this.spinner.hideSpinner(); - this.loading = false; - }); - } - nextPage(event: LazyLoadEvent) { this.page.pageSize = event.rows ?? 0; if (event.first != null && event.rows != null) this.page.pageIndex = event.first / event.rows; this.sort.sortColumn = event.sortField ?? undefined; - this.sort.sortDirection = event.sortOrder === 1 ? SortDirection.ASC : event.sortOrder === -1 ? SortDirection.DESC: SortDirection.ASC; + this.sort.sortDirection = event.sortOrder === 1 ? SortDirection.ASC : event.sortOrder === -1 ? SortDirection.DESC : SortDirection.ASC; this.loadNextPage(); } diff --git a/kdb-web/src/app/modules/view/server/profile/profile.component.ts b/kdb-web/src/app/modules/view/server/profile/profile.component.ts index 23f62959..8ecafa58 100644 --- a/kdb-web/src/app/modules/view/server/profile/profile.component.ts +++ b/kdb-web/src/app/modules/view/server/profile/profile.component.ts @@ -5,6 +5,11 @@ import { UserListQuery } from "../../../../models/graphql/query.model"; import { SpinnerService } from "../../../../services/spinner/spinner.service"; import { DataService } from "../../../../services/data/data.service"; import { User } from "../../../../models/data/user.model"; +import { UserDTO } from "../../../../models/auth/auth-user.dto"; +import { AuthService } from "src/app/services/auth/auth.service"; +import { SidebarService } from "../../../../services/sidebar/sidebar.service"; +import { ToastService } from "src/app/services/toast/toast.service"; +import { TranslateService } from "@ngx-translate/core"; @Component({ selector: "app-profile", @@ -19,11 +24,15 @@ export class ProfileComponent implements OnInit { private route: ActivatedRoute, private router: Router, private spinner: SpinnerService, - private data: DataService + private sidebar: SidebarService, + private data: DataService, + private auth: AuthService, + private toast: ToastService, + private translate: TranslateService ) { } - ngOnInit() { + async ngOnInit() { this.data.getServerFromRoute(this.route); this.spinner.showSpinner(); @@ -33,10 +42,18 @@ export class ProfileComponent implements OnInit { return; } + + let authUser = await this.auth.getLoggedInUser(); + let user: UserDTO | null = authUser?.users?.find(u => u.server == this.sidebar.server$.value?.id) ?? null; + if (!user || user?.id != this.route.snapshot.params["memberId"] && !user?.isModerator) { + this.toast.error(this.translate.instant("view.server.profile.permission_denied"), this.translate.instant("view.server.profile.permission_denied_d")); + this.router.navigate(["/server", this.sidebar.server$.value?.id]); + return; + } + this.data.query(Queries.singleUserQuery, { filter: { - id: this.route.snapshot.params["memberId"], - leftServer: false + id: this.route.snapshot.params["memberId"] } }, function(data: UserListQuery) { @@ -44,7 +61,6 @@ export class ProfileComponent implements OnInit { } ).subscribe(user => { this.user = user; - console.log(this.user); this.spinner.hideSpinner(); }); } diff --git a/kdb-web/src/app/services/data/data.service.ts b/kdb-web/src/app/services/data/data.service.ts index 79e9d401..e0c36ef1 100644 --- a/kdb-web/src/app/services/data/data.service.ts +++ b/kdb-web/src/app/services/data/data.service.ts @@ -44,18 +44,6 @@ export class DataService { }); } - // query(query: string, variables: object = {}): Observable { - // return this.http.post(`${this.appsettings.getApiURL()}/api/graphql`, - // JSON.stringify({ - // query, variables - // }), { - // headers: new HttpHeaders({ - // "Content-Type": "application/json" - // }) - // } - // ); - // } - public query(query: string, variables?: Variables, f?: Function): Observable { return this.http .post<{ data: T }>(`${this.appsettings.getApiURL()}/api/graphql`, { diff --git a/kdb-web/src/app/services/sidebar/sidebar.service.ts b/kdb-web/src/app/services/sidebar/sidebar.service.ts index a133590f..5a7a23a6 100644 --- a/kdb-web/src/app/services/sidebar/sidebar.service.ts +++ b/kdb-web/src/app/services/sidebar/sidebar.service.ts @@ -114,7 +114,7 @@ export class SidebarService { if (this.server$.value) { this.serverMenu.visible = true; - this.serverMembers.visible = !!user?.isAdmin; + this.serverMembers.visible = !!user?.isModerator; } else { this.serverMenu.visible = false; } diff --git a/kdb-web/src/assets/i18n/de.json b/kdb-web/src/assets/i18n/de.json index eb08e13a..c582b6c1 100644 --- a/kdb-web/src/assets/i18n/de.json +++ b/kdb-web/src/assets/i18n/de.json @@ -182,7 +182,9 @@ "time": "Zeit", "joined_at": "Beigetreten am", "leaved_at": "Verlassen am" - } + }, + "permission_denied": "Zugriff Verweigert!", + "permission_denied_d": "Du musst Moderator sein, um andere Profile sehen zu können!" }, "members": { "header": "Mitglieder", @@ -196,6 +198,7 @@ "name": "Name", "xp": "XP", "ontime": "Ontime", + "left_server": "Aktiv", "level": "Level", "actions": "Aktionen" },