Skip to content

Commit

Permalink
related to #2158 add main user
Browse files Browse the repository at this point in the history
- remove $conf['webmaster_id'] from default config and enter it into the database
- add warning message in user list page if $conf['webmaster_id'] exist
- add method api for set a main user
- show the main user in user list page
- add icon in user modal for main user
  • Loading branch information
LintyDev committed May 30, 2024
1 parent e9f4a64 commit ceb661d
Show file tree
Hide file tree
Showing 11 changed files with 512 additions and 44 deletions.
203 changes: 192 additions & 11 deletions admin/themes/default/js/user_list.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const color_icons = ["icon-red", "icon-blue", "icon-yellow", "icon-purple", "icon-green"];
const status_arr = ['webmaster', 'admin', 'normal', 'generic', 'guest'];
const level_arr = ['0', '1', '2', '4', '8'];
const king_template = '<span class="icon-king" id="the_king"></span>';
let current_users = [];
let guest_id = 0;
let guest_user = {};
Expand All @@ -20,14 +21,16 @@ let plugins_set_functions = {};
let plugins_get_functions = {};
let plugins_load = [];
let plugins_users_infos_table = [];
let owner_username = '';
/*----------------
Escape of pop-in
----------------*/

//get out of pop in via escape key
$(document).on('keydown', function (e) {
if ( e.keyCode === 27) { // ESC button
$("#UserList").fadeOut();
hide_modals();
close_user_list();
}
});

Expand Down Expand Up @@ -82,16 +85,6 @@ function isSelectionMode() {
return $("#toggleSelectionMode").is(":checked")
}

function reset_password_modals() {
$('.user-property-password-change').hide();
$('.user-property-password-change-inputs').hide();
$('#edit_password_success_change').hide();
$('#edit_password_result_mail').hide();
$('#edit_password_result_mail_copy').hide();
$('.user-property-password-choice').show();
}


$( document ).ready(function() {
$(".user-property-register").tipTip({
maxWidth: "300px",
Expand Down Expand Up @@ -1162,6 +1155,110 @@ function plugin_add_tab_in_user_modal(tab_name, content_id, users_table=null, se
}
}

function reset_password_modals() {
$('.user-property-password-change').hide();
$('.user-property-password-change-inputs').hide();
$('#edit_password_success_change').hide();
$('#edit_password_result_mail').hide();
$('#edit_password_result_mail_copy').hide();
$('.user-property-password-choice').show();
}

function reset_username_modals() {
$('.edit-username-success').hide();
$('.user-property-username-change-input').show();
}

function reset_main_user_modals() {
$('#main_user_rewrite').val('');
$('#main_user_rewrite_icon').removeClass('icon-ok icon-green icon-cancel icon-red');
$('.main-user-rewrite').hide();
$('.main-user-validate').hide();
$('.main-user-success').hide();
$('.main-user-proceed').show();
}

function hide_modals() {
$('.user-property-username-change').hide();
$('.user-property-password-change').hide();
$('.user-property-main-user-change').hide();
reset_password_modals();
reset_username_modals();
reset_main_user_modals();
}

function display_long_string(username) {
const formatedUsername = username.length > 20 ? username.slice(0, 17) + '...' : username;
return formatedUsername;
}

function generate_random_string() {
const string = 'abcdefghijkmlnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
let result = '';
let c = 5;
for (let i = 0; i < c; i++) {
result += string.charAt(Math.floor(Math.random() * string.length));
}
return result;
}

/* ---------------
Who is the king functions (main user)
----------------*/
function open_main_user_modal(user_to_edit) {
const modal = $('.user-property-main-user-change');
reset_main_user_modals();
$('.main-user-proceed-desc').html(sprintf(mainUserContinue, `<b>${display_long_string(user_to_edit.username)}</b>`, `<b>${display_long_string(owner_username)}</b>`));
modal.fadeIn();

// set event
$('.main-user-btn-proceed').off('click').on('click', function() {
const gen_string = generate_random_string();
$('.main-user-rewrite-desc').html(sprintf(mainUserRewrite, `<b>${gen_string}</b>`) + ' :');
$('.main-user-proceed').hide();
$('.main-user-rewrite').fadeIn();
event_check_string_main_user(user_to_edit.username, user_to_edit.id, gen_string);
});

$('.user-property-main-user-cancel, #main_user_success_close').off('click').on('click', function() {
modal.fadeOut();
});

}

function set_main_user_success() {
const indexKey = current_users.findIndex(u => u.id === owner_id);
const new_main = $('.user-container[key="' + indexKey + '"]').find('.user-container-username');
let king = $('#the_king');
if (!king.length) {
king = $(king_template);
}
king.appendTo(new_main);
$('.main-user-validate').hide();
$('.main-user-success').fadeIn();
}

function event_check_string_main_user(new_main_username, new_main_id, stringToCheck) {
const icon = $('#main_user_rewrite_icon');
$('#main_user_rewrite').off('keyup').on('keyup', function() {
icon.removeClass('icon-ok icon-green').addClass('icon-cancel icon-red');
if (stringToCheck === $(this).val()) {
icon.removeClass('icon-cancel icon-red').addClass('icon-ok icon-green');
$('.main-user-validate-desc').html(sprintf(mainUserValidate, `<b>${display_long_string(owner_username)}</b>`, `<b>${display_long_string(new_main_username)}</b>`));
$('.main-user-success-desc').html(sprintf(mainUserSuccess, display_long_string(new_main_username)));
$('.main-user-rewrite').hide();
$('.main-user-validate').fadeIn();
event_validate_main_user(new_main_username, new_main_id);
}
});
}

function event_validate_main_user(new_main_username, user_id) {
$('.main-user-btn-validate').off('click').on('click', function() {
set_main_user(user_id, new_main_username);
});
}

/*-----------------------
Generate User Containers
-----------------------*/
Expand Down Expand Up @@ -1237,6 +1334,9 @@ function fill_container_user_info(container, user_index) {
let registration_dates = user.registration_date.split(' ');
container.attr('key', user_index);
container.find(".user-container-username span").html(user.username);
if(user.id === owner_id) {
container.find(".user-container-username").append(king_template);
}
container.find(".user-container-initials span").html(get_initials(user.username)).addClass(color_icons[user.id % 5]);
container.find(".user-container-status span").html(status_to_str[user.status]);
container.find(".user-container-email span").html(user.email);
Expand Down Expand Up @@ -1533,6 +1633,7 @@ function fill_user_edit(user_to_edit) {
fill_user_edit_preferences(user_to_edit, pop_in);
fill_user_edit_update(user_to_edit, pop_in);
fill_user_edit_permissions(user_to_edit, pop_in);
fill_who_is_the_king(user_to_edit, pop_in);

// plugins get function
if (Object.keys(plugins_get_functions).length > 0) {
Expand Down Expand Up @@ -1560,6 +1661,53 @@ function fill_guest_edit() {
fill_user_edit_update(user_to_edit, pop_in);
}

function fill_who_is_the_king(user_to_edit, pop_in) {
const who_is_the_king = pop_in.find("#who_is_the_king");
// By default I'm an admin and I only see who is the Main User
who_is_the_king
.removeClass('princes-of-this-piwigo king-of-this-piwigo can-change')
.addClass('royal-court-of-this-piwigo cannot-change')
.attr('title', mainAskWebmaster)
.tipTip();
who_is_the_king.off('click');
// I'm an webmaster
if (connected_user_status === 'webmaster') {
// check user to edit status
switch (user_to_edit.status) {
// user to edit is an webmaster
case 'webmaster':
who_is_the_king
.removeClass('royal-court-of-this-piwigo king-of-this-piwigo cannot-change')
.addClass('princes-of-this-piwigo can-change')
.attr('title', mainUserSet)
.tipTip();
if (!is_owner(user_to_edit.id)) {
who_is_the_king.off('click').on('click', function() {
open_main_user_modal(user_to_edit);
});
}
break;
// if user to edit is not an webmaster he cannot be set as a main user
default:
who_is_the_king
.removeClass('princes-of-this-piwigo king-of-this-piwigo')
.addClass('royal-court-of-this-piwigo')
.attr('title', mainUserUpgradeWebmaster)
.tipTip();
break;
}
}

// Main User also the King
if (is_owner(user_to_edit.id)) {
who_is_the_king
.removeClass('princes-of-this-piwigo royal-court-of-this-piwigo can-change')
.addClass('king-of-this-piwigo cannot-change')
.attr('title', mainUserStr)
.tipTip();
}
}

/*-------------------
Fill data for setInfo
-------------------*/
Expand Down Expand Up @@ -1795,6 +1943,9 @@ function update_user_info() {
});
}

// update who is the king
fill_who_is_the_king(result_user, $('#UserList'));

} else if (data.stat === 'fail') {
$("#UserList .update-user-fail").html(data.message);
$("#UserList .update-user-fail").fadeIn();
Expand Down Expand Up @@ -2080,3 +2231,33 @@ function send_link_password(email, username, user_id, send_by_mail) {
},
});
}

function set_main_user(user_id, new_username) {
$.ajax({
url: 'ws.php?format=json',
dataType: 'json',
type: 'POST',
data: {
method: 'pwg.users.setMainUser',
user_id: user_id,
pwg_token: pwg_token
},
success: function(res) {
if('ok' === res.stat) {
$('#who_is_the_king')
.off('click')
.removeClass('princes-of-this-piwigo royal-court-of-this-piwigo can-change')
.addClass('king-of-this-piwigo cannot-change')
.attr('title', mainUserStr)
.tipTip();

owner_id = user_id;
owner_username = new_username;
set_main_user_success();
}
},
error: function(err) {
console.log(err);
}
});
}
Loading

0 comments on commit ceb661d

Please sign in to comment.