From a2ae5e3d153726409e97a8d474322bb19a8fc3dc Mon Sep 17 00:00:00 2001 From: flashwave Date: Thu, 6 Jun 2019 22:09:27 +0200 Subject: [PATCH] Made settings multi-page again. --- assets/less/settings/wrapper.less | 9 +- public/settings.php | 208 +---------------------- public/settings/account.php | 129 ++++++++++++++ public/settings/index.php | 11 ++ public/settings/logs.php | 41 +++++ public/settings/sessions.php | 63 +++++++ src/url.php | 11 +- templates/settings/account.twig | 144 ++++++++++++++++ templates/settings/logs.twig | 57 +++++++ templates/settings/master.twig | 51 ++++++ templates/settings/sessions.twig | 43 +++++ templates/user/macros.twig | 4 +- templates/user/settings.twig | 271 ------------------------------ 13 files changed, 552 insertions(+), 490 deletions(-) create mode 100644 public/settings/account.php create mode 100644 public/settings/index.php create mode 100644 public/settings/logs.php create mode 100644 public/settings/sessions.php create mode 100644 templates/settings/account.twig create mode 100644 templates/settings/logs.twig create mode 100644 templates/settings/master.twig create mode 100644 templates/settings/sessions.twig delete mode 100644 templates/user/settings.twig diff --git a/assets/less/settings/wrapper.less b/assets/less/settings/wrapper.less index aa263c60..9170b430 100644 --- a/assets/less/settings/wrapper.less +++ b/assets/less/settings/wrapper.less @@ -15,9 +15,7 @@ &__menu { width: 280px; - position: sticky; - top: 2px; - margin: 0 2px 2px; + margin-right: 2px; @media (max-width: @site-mobile-width) { width: 100%; @@ -38,5 +36,10 @@ &:hover { background-color: var(--background-colour-translucent-9); } + + @media (max-width: @site-mobile-width) { + display: inline-block; + padding: 4px 10px; + } } } diff --git a/public/settings.php b/public/settings.php index 53c22508..875351a3 100644 --- a/public/settings.php +++ b/public/settings.php @@ -1,210 +1,4 @@ 0 && user_role_has($currentUserId, $roleId)) { - switch ($_POST['role']['mode'] ?? '') { - case 'display': - user_role_set_display($currentUserId, $roleId); - break; - - case 'leave': - if (user_role_can_leave($roleId)) { - user_role_remove($currentUserId, $roleId); - } else { - $errors[] = "You're not allow to leave this role, an administrator has to remove it for you."; - } - break; - } - } else { - $errors[] = "You're trying to modify a role that hasn't been assigned to you."; - } - } - - if (isset($_POST['tfa']['enable']) && (bool)$twoFactorInfo['totp_enabled'] !== (bool)$_POST['tfa']['enable']) { - if ((bool)$_POST['tfa']['enable']) { - $tfaKey = totp_generate_key(); - - tpl_vars([ - 'settings_2fa_code' => $tfaKey, - 'settings_2fa_image' => totp_qrcode(totp_uri( - sprintf( - '%s:%s', - config_get_default('Misuzu', 'Site', 'name'), - $twoFactorInfo['username'] - ), - $tfaKey, - $_SERVER['HTTP_HOST'] - )), - ]); - - user_totp_update($currentUserId, $tfaKey); - } else { - user_totp_update($currentUserId, null); - } - - $twoFactorInfo['totp_enabled'] = !$twoFactorInfo['totp_enabled']; - } - - if (!empty($_POST['current_password'])) { - if (!user_password_verify_db($currentUserId, $_POST['current_password'] ?? '')) { - $errors[] = 'Your password was incorrect.'; - } else { - // Changing e-mail - if (!empty($_POST['email']['new'])) { - if (empty($_POST['email']['confirm']) || $_POST['email']['new'] !== $_POST['email']['confirm']) { - $errors[] = 'The addresses you entered did not match each other.'; - } elseif ($currentEmail === mb_strtolower($_POST['email']['confirm'])) { - $errors[] = 'This is already your e-mail address!'; - } else { - $checkMail = user_validate_email($_POST['email']['new'], true); - - if ($checkMail !== '') { - switch ($checkMail) { - case 'dns': - $errors[] = 'No valid MX record exists for this domain.'; - break; - - case 'format': - $errors[] = 'The given e-mail address was incorrectly formatted.'; - break; - - case 'in-use': - $errors[] = 'This e-mail address is already in use.'; - break; - - default: - $errors[] = 'Unknown e-mail validation error.'; - } - } else { - user_email_set($currentUserId, $_POST['email']['new']); - audit_log(MSZ_AUDIT_PERSONAL_EMAIL_CHANGE, $currentUserId, [ - $_POST['email']['new'], - ]); - } - } - } - - // Changing password - if (!empty($_POST['password']['new'])) { - if (empty($_POST['password']['confirm']) || $_POST['password']['new'] !== $_POST['password']['confirm']) { - $errors[] = 'The new passwords you entered did not match each other.'; - } else { - $checkPassword = user_validate_password($_POST['password']['new']); - - if ($checkPassword !== '') { - $errors[] = 'The given passwords was too weak.'; - } else { - user_password_set($currentUserId, $_POST['password']['new']); - audit_log(MSZ_AUDIT_PERSONAL_PASSWORD_CHANGE, $currentUserId); - } - } - } - } - } - } -} - -$sessions = [ - 'list' => [], - 'active' => user_session_current('session_id'), - 'pagination' => pagination_create(user_session_count($currentUserId), 15), -]; - -$logins = [ - 'list' => [], - 'pagination' => pagination_create(user_login_attempts_count($currentUserId), 15), -]; - -$logs = [ - 'list' => [], - 'pagination' => pagination_create(audit_log_count($currentUserId), 15), - 'strings' => MSZ_AUDIT_LOG_STRINGS, -]; - -foreach (['sessions', 'logins', 'logs'] as $section) { - if (!pagination_is_valid_offset(pagination_offset(($$section)['pagination'], pagination_param("{$section}_page")))) { - ($$section)['pagination']['offset'] = 0; - ($$section)['pagination']['page'] = 1; - } -} - -$sessions['list'] = user_session_list( - $sessions['pagination']['offset'], - $sessions['pagination']['range'], - $currentUserId -); -$logins['list'] = user_login_attempts_list( - $logins['pagination']['offset'], - $logins['pagination']['range'], - $currentUserId -); -$logs['list'] = audit_log_list( - $logs['pagination']['offset'], - $logs['pagination']['range'], - $currentUserId -); - -$userRoles = user_role_all_user($currentUserId); - -echo tpl_render('user.settings', [ - 'errors' => $errors, - 'current_email' => $currentEmail, - 'sessions' => $sessions, - 'logins' => $logins, - 'logs' => $logs, - 'user_roles' => $userRoles, - 'user_display_role' => user_role_get_display($currentUserId), - 'is_restricted' => $isRestricted, - 'settings_2fa_enabled' => $twoFactorInfo['totp_enabled'], -]); +header('Location: ' . url('settings-index')); diff --git a/public/settings/account.php b/public/settings/account.php new file mode 100644 index 00000000..88c579f7 --- /dev/null +++ b/public/settings/account.php @@ -0,0 +1,129 @@ + 0 && user_role_has($currentUserId, $roleId)) { + switch ($_POST['role']['mode'] ?? '') { + case 'display': + user_role_set_display($currentUserId, $roleId); + break; + + case 'leave': + if (user_role_can_leave($roleId)) { + user_role_remove($currentUserId, $roleId); + } else { + $errors[] = "You're not allow to leave this role, an administrator has to remove it for you."; + } + break; + } + } else { + $errors[] = "You're trying to modify a role that hasn't been assigned to you."; + } +} + +if($isVerifiedRequest && isset($_POST['tfa']['enable']) && (bool)$twoFactorInfo['totp_enabled'] !== (bool)$_POST['tfa']['enable']) { + if((bool)$_POST['tfa']['enable']) { + $tfaKey = totp_generate_key(); + + tpl_vars([ + 'settings_2fa_code' => $tfaKey, + 'settings_2fa_image' => totp_qrcode(totp_uri( + sprintf( + '%s:%s', + config_get_default('Misuzu', 'Site', 'name'), + $twoFactorInfo['username'] + ), + $tfaKey, + $_SERVER['HTTP_HOST'] + )), + ]); + + user_totp_update($currentUserId, $tfaKey); + } else { + user_totp_update($currentUserId, null); + } + + $twoFactorInfo['totp_enabled'] = !$twoFactorInfo['totp_enabled']; +} + +if($isVerifiedRequest && !empty($_POST['current_password'])) { + if(!user_password_verify_db($currentUserId, $_POST['current_password'] ?? '')) { + $errors[] = 'Your password was incorrect.'; + } else { + // Changing e-mail + if(!empty($_POST['email']['new'])) { + if(empty($_POST['email']['confirm']) || $_POST['email']['new'] !== $_POST['email']['confirm']) { + $errors[] = 'The addresses you entered did not match each other.'; + } elseif($currentEmail === mb_strtolower($_POST['email']['confirm'])) { + $errors[] = 'This is already your e-mail address!'; + } else { + $checkMail = user_validate_email($_POST['email']['new'], true); + + if ($checkMail !== '') { + switch ($checkMail) { + case 'dns': + $errors[] = 'No valid MX record exists for this domain.'; + break; + + case 'format': + $errors[] = 'The given e-mail address was incorrectly formatted.'; + break; + + case 'in-use': + $errors[] = 'This e-mail address is already in use.'; + break; + + default: + $errors[] = 'Unknown e-mail validation error.'; + } + } else { + user_email_set($currentUserId, $_POST['email']['new']); + audit_log(MSZ_AUDIT_PERSONAL_EMAIL_CHANGE, $currentUserId, [ + $_POST['email']['new'], + ]); + } + } + } + + // Changing password + if(!empty($_POST['password']['new'])) { + if(empty($_POST['password']['confirm']) || $_POST['password']['new'] !== $_POST['password']['confirm']) { + $errors[] = 'The new passwords you entered did not match each other.'; + } else { + $checkPassword = user_validate_password($_POST['password']['new']); + + if($checkPassword !== '') { + $errors[] = 'The given passwords was too weak.'; + } else { + user_password_set($currentUserId, $_POST['password']['new']); + audit_log(MSZ_AUDIT_PERSONAL_PASSWORD_CHANGE, $currentUserId); + } + } + } + } +} + +$userRoles = user_role_all_user($currentUserId); + +echo tpl_render('settings.account', [ + 'errors' => $errors, + 'current_email' => $currentEmail, + 'user_roles' => $userRoles, + 'user_display_role' => user_role_get_display($currentUserId), + 'is_restricted' => $isRestricted, + 'settings_2fa_enabled' => $twoFactorInfo['totp_enabled'], +]); diff --git a/public/settings/index.php b/public/settings/index.php new file mode 100644 index 00000000..326d82d5 --- /dev/null +++ b/public/settings/index.php @@ -0,0 +1,11 @@ + $loginHistoryList, + 'login_history_pagination' => $loginHistoryPagination, + 'account_log_list' => $accountLogList, + 'account_log_pagination' => $accountLogPagination, + 'account_log_strings' => MSZ_AUDIT_LOG_STRINGS, +]); diff --git a/public/settings/sessions.php b/public/settings/sessions.php new file mode 100644 index 00000000..5df13290 --- /dev/null +++ b/public/settings/sessions.php @@ -0,0 +1,63 @@ + $errors, + 'session_list' => $sessionList, + 'session_active_id' => $sessionActive, + 'session_pagination' => $sessionPagination, +]); diff --git a/src/url.php b/src/url.php index 1a3c3e2c..da097ec5 100644 --- a/src/url.php +++ b/src/url.php @@ -80,13 +80,10 @@ define('MSZ_URLS', [ 'user-relation-none' => ['/relations.php', ['u' => '', 'm' => '[MSZ_USER_RELATION_NONE]', 'c' => '{user_relation}']], 'user-relation-follow' => ['/relations.php', ['u' => '', 'm' => '[MSZ_USER_RELATION_FOLLOW]', 'c' => '{user_relation}']], - 'settings-index' => ['/settings.php'], - 'settings-account' => ['/settings.php', [], 'account'], - 'settings-roles' => ['/settings.php', [], 'roles'], - 'settings-tfa' => ['/settings.php', [], 'tfa'], - 'settings-sessions' => ['/settings.php', [], 'sessions'], - 'settings-login-attempts' => ['/settings.php', [], 'login-attempts'], - 'settings-account-log' => ['/settings.php', [], 'account-log'], + 'settings-index' => ['/settings'], + 'settings-account' => ['/settings/account.php'], + 'settings-sessions' => ['/settings/sessions.php'], + 'settings-logs' => ['/settings/logs.php'], 'comment-create' => ['/comments.php', ['m' => 'create']], 'comment-vote' => ['/comments.php', ['c' => '', 'csrf' => '{comments}', 'm' => 'vote', 'v' => '']], diff --git a/templates/settings/account.twig b/templates/settings/account.twig new file mode 100644 index 00000000..b9e7c14c --- /dev/null +++ b/templates/settings/account.twig @@ -0,0 +1,144 @@ +{% extends 'settings/master.twig' %} +{% from 'macros.twig' import container_title %} +{% from '_layout/input.twig' import input_hidden, input_csrf, input_text, input_select %} + +{% set title = 'Settings / Account' %} + +{% block settings_content %} +
+ {{ container_title(' Account') }} + {{ input_csrf('settings') }} + +
+

Here you can change your e-mail address and/or your password, please make sure your e-mail is accurate and your password is strong in order to protect your account. For convenience your current e-mail address is displayed. You are required to verify yourself by entering your current password to change either value.

+
+ + +
+ + {% if not is_restricted %} +
+ {{ container_title(' Roles') }} + +
+

This is a listing of the user roles you're a part of, you can select which you want to leave or which one you want to boast as your main role which will change your username colour accordingly.

+
+ +
+ {% for role in user_roles %} + {% set is_display_role = user_display_role == role.role_id %} + +
+
+
+ {{ role.role_name }} +
+ +
+ {{ role.role_description }} +
+ +
+ {{ input_csrf('settings') }} + {{ input_hidden('role[id]', role.role_id) }} + + + + +
+
+
+ {% endfor %} +
+
+ {% endif %} + +
+ {{ container_title(' Two Factor Authentication') }} + {{ input_csrf('settings') }} + +
+

Secure your account by requiring a second step during log in in the form of a time based code. You can use applications like Authy, Google or Microsoft Authenticator or other compliant TOTP applications.

+
+ +
+ {% if settings_2fa_image is defined and settings_2fa_code is defined %} +
+
+ {{ settings_2fa_code }} +
+ {{ settings_2fa_code }} +
+ {% endif %} + +
+ {% if settings_2fa_enabled %} +
+ Two Factor Authentication is enabled! +
+ + {% else %} +
+ Two Factor Authentication is disabled. +
+ + {% endif %} +
+
+
+{% endblock %} diff --git a/templates/settings/logs.twig b/templates/settings/logs.twig new file mode 100644 index 00000000..577d2755 --- /dev/null +++ b/templates/settings/logs.twig @@ -0,0 +1,57 @@ +{% extends 'settings/master.twig' %} +{% from 'macros.twig' import container_title, pagination %} +{% from 'user/macros.twig' import user_login_attempt, user_account_log %} + +{% set title = 'Settings / Logs' %} + +{% block settings_content %} +
+ {{ container_title(' Login History') }} + {% set lhpagination = pagination(login_history_pagination, url('settings-logs'), null, { + 'ap': account_log_pagination.page > 1 ? account_log_pagination.page : 0, + }, 'hp', 'login-history') %} + +
+

These are all the login attempts to your account. If any attempt that you don't recognise is marked as successful your account may be compromised, ask a staff member for advice in this case.

+
+ + +
+ +
+ {{ container_title(' Account Log') }} + {% set alpagination = pagination(account_log_pagination, url('settings-logs'), null, { + 'hp': login_history_pagination.page > 1 ? login_history_pagination.page : 0, + }, 'ap', 'account-log') %} + +
+

This is a log of all "important" actions that have been done using your account for your review. If you notice anything strange, please alert the staff.

+
+ + +
+{% endblock %} diff --git a/templates/settings/master.twig b/templates/settings/master.twig new file mode 100644 index 00000000..eb02adb0 --- /dev/null +++ b/templates/settings/master.twig @@ -0,0 +1,51 @@ +{% extends 'master.twig' %} +{% from 'macros.twig' import container_title %} + +{% set menu = [ + { + 'icon': 'fas fa-user fa-fw', + 'title': 'Account', + 'url': url('settings-account'), + }, + { + 'icon': 'fas fa-key fa-fw', + 'title': 'Sessions', + 'url': url('settings-sessions'), + }, + { + 'icon': 'fas fa-file-alt fa-fw', + 'title': 'Logs', + 'url': url('settings-logs'), + }, +] %} + +{% block content %} + {% if errors is defined and errors|length > 0 %} +
+
+ {% for error in errors %} + {{ error }} + {% endfor %} +
+
+ {% endif %} + +
+
+
+ {{ container_title(' Settings') }} + + {% for item in menu %} + + {{ item.title }} + + {% endfor %} +
+
+ +
+ {% block settings_content %} + {% endblock %} +
+
+{% endblock %} diff --git a/templates/settings/sessions.twig b/templates/settings/sessions.twig new file mode 100644 index 00000000..b226a417 --- /dev/null +++ b/templates/settings/sessions.twig @@ -0,0 +1,43 @@ +{% extends 'settings/master.twig' %} +{% from 'macros.twig' import container_title, pagination %} +{% from 'user/macros.twig' import user_session %} +{% from '_layout/input.twig' import input_hidden, input_csrf %} + +{% set title = 'Settings / Sessions' %} + +{% block settings_content %} +
+ {{ container_title(' Sessions') }} + + {% set spagination = pagination(session_pagination, url('settings-sessions')) %} + +
+

These are the active logins to your account, clicking the Kill button will force a logout on that session. Your current login is highlighted with a different colour so you don't accidentally force yourself to logout.

+
+ +
+
+ {{ input_csrf('user_session') }} + {{ input_hidden('session', 'all') }} + + +
+ +
+ {{ spagination }} +
+ +
+ {% for session in session_list %} + {{ user_session(session, session.session_id == session_active_id) }} + {% endfor %} +
+ +
+ {{ spagination }} +
+
+
+{% endblock %} diff --git a/templates/user/macros.twig b/templates/user/macros.twig index a8ab8273..7910a1a1 100644 --- a/templates/user/macros.twig +++ b/templates/user/macros.twig @@ -131,8 +131,8 @@ {{ browser.browser }} on {{ browser.platform }} -
- {{ input_csrf('settings') }} + + {{ input_csrf('user_session') }} {{ input_hidden('session[]', session.session_id) }} - - - - -
- - {% if not is_restricted %} -
- {{ container_title(' Roles') }} - -
-

This is a listing of the user roles you're a part of, you can select which you want to leave or which one you want to boast as your main role which will change your username colour accordingly.

-
- -
- {% for role in user_roles %} - {% set is_display_role = user_display_role == role.role_id %} - -
-
-
- {{ role.role_name }} -
- -
- {{ role.role_description }} -
- -
- {{ input_csrf('settings') }} - {{ input_hidden('role[id]', role.role_id) }} - - - - -
-
-
- {% endfor %} -
-
- {% endif %} - -
- {{ container_title(' Two Factor Authentication') }} - {{ input_csrf('settings') }} - -
-

Secure your account by requiring a second step during log in in the form of a time based code. You can use applications like Authy, Google or Microsoft Authenticator or other compliant TOTP applications.

-
- -
- {% if settings_2fa_image is defined and settings_2fa_code is defined %} -
-
- {{ settings_2fa_code }} -
- {{ settings_2fa_code }} -
- {% endif %} - -
- {% if settings_2fa_enabled %} -
- Two Factor Authentication is enabled! -
- - {% else %} -
- Two Factor Authentication is disabled. -
- - {% endif %} -
-
-
- -
- {{ container_title(' Sessions') }} - {% set spagination = pagination(sessions.pagination, url('settings-index'), null, { - 'logins_page': logins.pagination.page > 1 ? logins.pagination.page : 0, - 'logs_page': logs.pagination.page > 1 ? logs.pagination.page : 0, - }, 'sessions_page', 'sessions') %} - -
-

These are the active logins to your account, clicking the Kill button will force a logout on that session. Your current login is highlighted with a different colour so you don't accidentally force yourself to logout.

-
- -
-
- {{ input_csrf('settings') }} - {{ input_hidden('session', 'all') }} - - -
- -
- {{ spagination }} -
- -
- {% for session in sessions.list %} - {{ user_session(session, session.session_id == sessions.active) }} - {% endfor %} -
- -
- {{ spagination }} -
-
-
- -
- {{ container_title(' Login Attempts') }} - {% set lhpagination = pagination(logins.pagination, url('settings-index'), null, { - 'sessions_page': sessions.pagination.page > 1 ? sessions.pagination.page : 0, - 'logs_page': logs.pagination.page > 1 ? logs.pagination.page : 0, - }, 'logins_page', 'login-attempts') %} - -
-

These are all the login attempts to your account. If any attempt that you don't recognise is marked as successful your account may be compromised, ask a staff member for advice in this case.

-
- - -
- -
- {{ container_title(' Account Log') }} - {% set alpagination = pagination(logs.pagination, url('settings-index'), null, { - 'sessions_page': sessions.pagination.page > 1 ? sessions.pagination.page : 0, - 'logins_page': logins.pagination.page > 1 ? logins.pagination.page : 0, - }, 'logs_page', 'account-log') %} - -
-

This is a log of all "important" actions that have been done using your account for your review. If you notice anything strange, please alert the staff.

-
- - -
- - -{% endblock %}