aboutsummaryrefslogtreecommitdiff
path: root/logic/src/admin/user.rs
blob: e27707701b026b45ef8d98459f077456d2081c42 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*
    This file is part of jellything (https://codeberg.org/metamuffin/jellything)
    which is licensed under the GNU Affero General Public License (version 3); see /COPYING.
    Copyright (C) 2025 metamuffin <metamuffin.org>
*/

use crate::{DATABASE, session::AdminSession};
use anyhow::{Result, anyhow};
use jellycommon::{
    api::ApiAdminUsersResponse,
    user::{User, UserPermission},
};

pub fn admin_users(_session: &AdminSession) -> Result<ApiAdminUsersResponse> {
    // TODO dont return useless info like passwords
    Ok(ApiAdminUsersResponse {
        users: DATABASE.list_users()?,
    })
}
pub fn get_user(_session: &AdminSession, username: &str) -> Result<User> {
    DATABASE
        .get_user(username)?
        .ok_or(anyhow!("user not found"))
}
pub fn delete_user(_session: &AdminSession, username: &str) -> Result<()> {
    if !DATABASE.delete_user(&username)? {
        Err(anyhow!("user did not exist"))?;
    }
    Ok(())
}

pub enum GrantState {
    Grant,
    Revoke,
    Unset,
}
pub fn update_user_perms(
    _session: &AdminSession,
    username: &str,
    perm: UserPermission,
    action: GrantState,
) -> Result<()> {
    DATABASE.update_user(username, |user| {
        match action {
            GrantState::Grant => drop(user.permissions.0.insert(perm.clone(), true)),
            GrantState::Revoke => drop(user.permissions.0.insert(perm.clone(), false)),
            GrantState::Unset => drop(user.permissions.0.remove(&perm)),
        }
        Ok(())
    })
}