make admin ok

This commit is contained in:
simplonco
2023-01-15 19:50:27 +01:00
parent 164581b655
commit 77c420a174
6 changed files with 57 additions and 18 deletions

View File

@@ -87,21 +87,18 @@ export class ChatController {
@UseGuards(AuthenticateGuard) @UseGuards(AuthenticateGuard)
@UseGuards(TwoFactorGuard) @UseGuards(TwoFactorGuard)
@Get('isadmin') @Post('setadmin')
async isAdmin(@Req() req, @Res() res): Promise<void> async setAdmin(@Body('username') username: string, @Req() req, @Res() res): Promise<void>
{ {
printCaller("- in "); printCaller("- in ");
const room_name = await this.chatService.getCurrentRoomName(req.user.username); const current_room_name = await this.chatService.getCurrentRoomName(req.user.username);
const fields = ["admins"]; await this.chatService.setAdmin(req.user.username, username, current_room_name);
const room_db = await this.chatService.getRoomByName(room_name, fields);
const is_admin = room_db.admins.includes(req.user.username);
res.status(HttpStatus.OK).json({ is_admin: is_admin }); res.status(HttpStatus.OK).json({ message: `${username} is now admin in room ${current_room_name}` });
printCaller("- out "); printCaller("- out ");
} }
/*
@UseGuards(AuthenticateGuard) @UseGuards(AuthenticateGuard)
@UseGuards(TwoFactorGuard) @UseGuards(TwoFactorGuard)
@Get('isadmin') @Get('isadmin')
@@ -117,7 +114,6 @@ export class ChatController {
res.status(HttpStatus.OK).json({ is_admin: is_admin }); res.status(HttpStatus.OK).json({ is_admin: is_admin });
printCaller("- out "); printCaller("- out ");
} }
*/
@UseGuards(AuthenticateGuard) @UseGuards(AuthenticateGuard)
@UseGuards(TwoFactorGuard) @UseGuards(TwoFactorGuard)

View File

@@ -204,8 +204,9 @@ export class ChatService {
} }
room_db.allowed_users.push(username); room_db.allowed_users.push(username);
printCaller("-- out ");
await this.chatroomRepository.save(room_db); await this.chatroomRepository.save(room_db);
printCaller("-- out ");
} }
async setPassword(username: string, message: string, room: roomDto, old_password?: string): Promise<void> async setPassword(username: string, message: string, room: roomDto, old_password?: string): Promise<void>
@@ -274,6 +275,22 @@ export class ChatService {
printCaller("-- out "); printCaller("-- out ");
} }
async setAdmin(current_username: string, user_username: string, room_name: string): Promise<void>
{
printCaller("-- in ");
const room_db = await this.getRoomByName(room_name);
if (!room_db.admins.includes(current_username))
{
printCaller(`throw error: error: true, code: 'NOT_ADMIN', message: 'you cannot set someone else as admin, since you are not admin yourself'`);
throw new HttpException({ error: true, code: 'NOT_ADMIN', message: `you cannot set someone else as admin, since you are not admin yourself` }, HttpStatus.UNAUTHORIZED);
}
room_db.admins.push(user_username);
await this.chatroomRepository.save(room_db);
printCaller("-- out ");
}
/* ADDERS ************************************************* /* ADDERS *************************************************
*/ */

View File

@@ -164,6 +164,7 @@
width: 20px; width: 20px;
height: 2px; height: 2px;
background-color: black; background-color: black;
background-color: var(--lines_color);
} }

View File

@@ -57,6 +57,7 @@
</Button> </Button>
{/if} {/if}
{#if is_admin === true } {#if is_admin === true }
<p class="__center">you are admin in this room</p>
{#if $current_room.protection } {#if $current_room.protection }
<p class="__center">this room is password protected</p> <p class="__center">this room is password protected</p>
<Button new_layout="change_password"> <Button new_layout="change_password">

View File

@@ -1,11 +1,13 @@
<script> <script lang="ts">
import { layout, current_room, settings_user } from './Store_chat'; import { layout, current_room, settings_user } from './Store_chat';
import { get_is_admin } from './Request_rooms'; import { get_is_admin, make_admin } from './Request_rooms';
import type { FetchResponse } from './Types_chat';
import { to_print } from './Utils_chat'; import { to_print } from './Utils_chat';
import Button from './Element_button.svelte'; import Button from './Element_button.svelte';
import { push } from "svelte-spa-router"; import { push } from "svelte-spa-router";
import { invited_username } from '../store_invitation'; import { invited_username } from '../store_invitation';
import Warning from './Element_warning.svelte';
export let back = ""; export let back = "";
@@ -15,6 +17,9 @@
let is_admin = false; let is_admin = false;
get_is_admin().then(response => is_admin = response); get_is_admin().then(response => is_admin = response);
let response: FetchResponse;
let show_error = false;
function game_invitation() function game_invitation()
{ {
to_print("in game_invitation"); to_print("in game_invitation");
@@ -26,13 +31,18 @@
{ {
to_print("in view_profile"); to_print("in view_profile");
} }
function make_admin() async function make_user_admin()
{ {
to_print("in make_admin"); to_print("in make_user_admin");
response = await make_admin($settings_user);
//show errors
if (response.status >= 300 || response.error)
show_error = response.error;
} }
function mute_user() async function ban_mute_user()
{ {
to_print("in mute_user"); to_print("in ban_mute_user");
} }
</script> </script>
@@ -63,6 +73,9 @@
<!-- panel_user --> <!-- panel_user -->
<div class="panel panel_user __border_top"> <div class="panel panel_user __border_top">
{#if show_error}
<Warning content={response.message}/>
{/if}
<p class="__center">user options :</p> <p class="__center">user options :</p>
<Button on:click={view_profile}> <Button on:click={view_profile}>
view profile view profile
@@ -72,10 +85,10 @@
</Button> </Button>
{#if back === "room_set"} {#if back === "room_set"}
<Button on:click={make_admin}> <Button on:click={make_user_admin}>
make admin make admin
</Button> </Button>
<Button on:click={mute_user}> <Button on:click={ban_mute_user}>
{mute} {mute}
</Button> </Button>
{/if} {/if}

View File

@@ -165,6 +165,17 @@ export async function leave_room(): Promise<void>
let response: FetchResponse = await fetch_chat_request('leave', FetchMethod.DELETE); let response: FetchResponse = await fetch_chat_request('leave', FetchMethod.DELETE);
} }
export async function make_admin(username): Promise<boolean>
{
to_print("in is_admin");
to_print("username sent to setadmin:", username);
let response: FetchResponse = await fetch_chat_request('setadmin', FetchMethod.POST, {username: username} );
to_print("response from setadmin:", response);
return response;
}
export async function get_is_admin(): Promise<boolean> export async function get_is_admin(): Promise<boolean>
{ {
to_print("in is_admin"); to_print("in is_admin");