173 lines
		
	
	
		
			5.7 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
			
		
		
	
	
			173 lines
		
	
	
		
			5.7 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
function fetch_user_groups(nickname, cb) {
 | 
						|
    let req = {
 | 
						|
        action: "get_user_groups",
 | 
						|
        nickname: nickname
 | 
						|
    };
 | 
						|
    request(req).then(resp => {
 | 
						|
        let groups = JSON.parse(resp);
 | 
						|
        let groupsJoined = groups.join(", ");
 | 
						|
        cb(groupsJoined);
 | 
						|
    });
 | 
						|
}
 | 
						|
 | 
						|
function list_all_users() {
 | 
						|
    let tbody = document.getElementById("user_manager_table_body");
 | 
						|
    tbody.innerHTML = "";
 | 
						|
    let req = {action: "get_all_users"};
 | 
						|
    request(req).then(resp => {
 | 
						|
        let users = JSON.parse(resp);
 | 
						|
        for (let i = 0; i < users.length; i++) {
 | 
						|
            let u = users[i];
 | 
						|
            let row = document.createElement("tr");
 | 
						|
            row.id = "row_" + u["nickname"];
 | 
						|
            let chkbox = document.createElement("input");
 | 
						|
            chkbox.type = "checkbox";
 | 
						|
            chkbox.name = "user_chkbox";
 | 
						|
            chkbox.user = u;
 | 
						|
            chkbox.id = "chk_" + u["nickname"];
 | 
						|
            chkbox.addEventListener("input", () => {
 | 
						|
                highlight_row(u["nickname"]);
 | 
						|
            });
 | 
						|
            let tdChkBox = document.createElement("td");
 | 
						|
            tdChkBox.appendChild(chkbox);
 | 
						|
            tdChkBox.classList.add("checkbox");
 | 
						|
            let tdNickName = create_table_cell(u["nickname"]);
 | 
						|
            let tdRealName = create_table_cell(u["realname"]);
 | 
						|
            let tdGroups = create_table_cell(null);
 | 
						|
            let fetchGroupsS = document.createElement("span");
 | 
						|
            fetchGroupsS.innerHTML = "📥";
 | 
						|
            fetchGroupsS.style.cursor = "pointer";
 | 
						|
            fetchGroupsS.addEventListener("click", (evt) => {
 | 
						|
                let nickname = u["nickname"];
 | 
						|
                fetch_user_groups(nickname, (groups) => {
 | 
						|
                   tdGroups.innerText = groups;
 | 
						|
                });
 | 
						|
 | 
						|
                evt.stopPropagation();
 | 
						|
            });
 | 
						|
            tdGroups.append(fetchGroupsS);
 | 
						|
 | 
						|
            let tdPrivilege = create_table_cell(u["privilege"]);
 | 
						|
            row.append(tdChkBox, tdNickName, tdRealName, tdGroups, tdPrivilege);
 | 
						|
            tbody.appendChild(row);
 | 
						|
 | 
						|
            let edit_user_action = () => {
 | 
						|
                if (get_selected_users().length === 0) {
 | 
						|
                    create_edit_user(u);
 | 
						|
                }
 | 
						|
            };
 | 
						|
 | 
						|
            tdNickName.addEventListener("click", edit_user_action);
 | 
						|
            tdRealName.addEventListener("click", edit_user_action);
 | 
						|
            tdGroups.addEventListener("click", edit_user_action);
 | 
						|
            tdPrivilege.addEventListener("click", edit_user_action)
 | 
						|
        }
 | 
						|
    });
 | 
						|
}
 | 
						|
 | 
						|
function create_edit_user(user = null) {
 | 
						|
    let updating = user !== null;
 | 
						|
 | 
						|
    const generateRandomString = () => {
 | 
						|
        return Math.floor(Math.random() * Date.now()).toString(36);
 | 
						|
    };
 | 
						|
 | 
						|
    let nicknameF = document.getElementById("nickname");
 | 
						|
    let realnameF = document.getElementById("realname");
 | 
						|
    let passwordF = document.getElementById("password");
 | 
						|
    let groupsF = document.getElementById("groups");
 | 
						|
    let privilegeF = document.getElementById("privilege");
 | 
						|
    let submit_btn = document.getElementById("user_editor_submit_btn");
 | 
						|
 | 
						|
    if (updating) {
 | 
						|
        nicknameF.value = user["nickname"];
 | 
						|
        nicknameF.readOnly = true;
 | 
						|
        realnameF.value = user["realname"];
 | 
						|
        passwordF.type = "password";
 | 
						|
        passwordF.value = "";
 | 
						|
        passwordF.readOnly = false;
 | 
						|
        groupsF.value = "";
 | 
						|
        privilegeF.value = user["privilege"];
 | 
						|
        submit_btn.value = "Mentés"
 | 
						|
 | 
						|
        fetch_user_groups(user["nickname"], (groups) => {
 | 
						|
           groupsF.value = groups;
 | 
						|
        });
 | 
						|
    } else {
 | 
						|
        nicknameF.value = "";
 | 
						|
        nicknameF.readOnly = false;
 | 
						|
        realnameF.value = "";
 | 
						|
        passwordF.type = "text";
 | 
						|
        passwordF.value = generateRandomString();
 | 
						|
        passwordF.readOnly = true;
 | 
						|
        groupsF.value = "";
 | 
						|
        submit_btn.value = "Létrehozás"
 | 
						|
    }
 | 
						|
 | 
						|
    submit_btn.onclick = () => {
 | 
						|
        let nickname = nicknameF.value.trim();
 | 
						|
        if (nickname !== "") {
 | 
						|
            let req = {
 | 
						|
                action: updating ? "update_user" : "create_user",
 | 
						|
                nickname: nickname,
 | 
						|
                realname: realnameF.value.trim(),
 | 
						|
                password: passwordF.value,
 | 
						|
                groups: groupsF.value.trim(),
 | 
						|
                privilege: privilegeF.value
 | 
						|
            };
 | 
						|
            request(req).then(resp => {
 | 
						|
                list_all_users();
 | 
						|
            });
 | 
						|
            hide("user_editor_window");
 | 
						|
        }
 | 
						|
    };
 | 
						|
 | 
						|
    show("user_editor_window");
 | 
						|
}
 | 
						|
 | 
						|
function get_selected_users() {
 | 
						|
    let selected_chkboxes = document.getElementsByName("user_chkbox");
 | 
						|
    let selected_users = [];
 | 
						|
    selected_chkboxes.forEach((chkbox) => {
 | 
						|
        if (chkbox.checked) {
 | 
						|
            selected_users.push(chkbox.user);
 | 
						|
        }
 | 
						|
    });
 | 
						|
    return selected_users;
 | 
						|
}
 | 
						|
 | 
						|
function delete_users() {
 | 
						|
    let users = get_selected_users();
 | 
						|
    if (users.length === 0) {
 | 
						|
        return;
 | 
						|
    }
 | 
						|
    let user_nicknames = [];
 | 
						|
    users.forEach((u) => {
 | 
						|
        user_nicknames.push(u["nickname"]);
 | 
						|
    });
 | 
						|
    let msg = "Biztosan törölni kívánja a következő felhasználó(ka)t?\n\n" + user_nicknames.join(", ") + "\n\n"
 | 
						|
        + "A törlés nem vonható vissza!";
 | 
						|
    if (confirm(msg)) {
 | 
						|
        let req = {action: "delete_users", users: user_nicknames.join(",")};
 | 
						|
        request(req).then(resp => {
 | 
						|
            list_all_users();
 | 
						|
        });
 | 
						|
    }
 | 
						|
}
 | 
						|
 | 
						|
function hint_all_groups(target_element_id) {
 | 
						|
    const hintbox_insert_fn = (record) => {
 | 
						|
        let targetF = document.getElementById(target_element_id);
 | 
						|
        let groups = explode_sanitize_string_list(targetF.value);
 | 
						|
        groups.pop();
 | 
						|
        groups.push(record);
 | 
						|
        targetF.value = groups.join(", ");
 | 
						|
        close_hintbox(true);
 | 
						|
    }
 | 
						|
 | 
						|
    let req = {
 | 
						|
        action: "search_groups",
 | 
						|
    }
 | 
						|
 | 
						|
    open_hintbox_at(target_element_id, req, print_group_name, hintbox_insert_fn);
 | 
						|
} |