misuzu/public/settings/account.php

139 lines
4.9 KiB
PHP
Raw Normal View History

2019-06-06 20:09:27 +00:00
<?php
namespace Misuzu;
2020-05-21 15:05:30 +00:00
use Misuzu\AuditLog;
2020-05-28 17:52:31 +00:00
use Misuzu\Config;
use Misuzu\Users\User;
2020-06-04 18:48:01 +00:00
use Misuzu\Users\UserRole;
use Misuzu\Users\UserRoleNotFoundException;
2020-05-25 19:58:06 +00:00
use Misuzu\Users\UserSession;
2019-12-09 03:24:10 +00:00
use chillerlan\QRCode\QRCode;
use chillerlan\QRCode\QROptions;
2019-06-06 20:09:27 +00:00
require_once '../../misuzu.php';
2020-05-25 19:58:06 +00:00
if(!UserSession::hasCurrent()) {
2019-06-06 20:09:27 +00:00
echo render_error(401);
return;
}
$errors = [];
2020-05-20 18:09:38 +00:00
$currentUser = User::getCurrent();
$currentUserId = $currentUser->getId();
$isRestricted = $currentUser->hasActiveWarning();
2019-06-06 20:09:27 +00:00
$twoFactorInfo = user_totp_info($currentUserId);
2019-12-11 18:10:54 +00:00
$isVerifiedRequest = CSRF::validateRequest();
2019-06-06 20:09:27 +00:00
if(!$isRestricted && $isVerifiedRequest && !empty($_POST['role'])) {
2020-06-04 18:48:01 +00:00
try {
$roleInfo = UserRole::byId((int)($_POST['role']['id'] ?? 0));
} catch(UserRoleNotFoundException $ex) {}
2019-06-06 20:09:27 +00:00
2020-06-04 18:48:01 +00:00
if(empty($roleInfo) || !$currentUser->hasRole($roleInfo))
$errors[] = "You're trying to modify a role that hasn't been assigned to you.";
else {
2019-06-10 17:04:53 +00:00
switch($_POST['role']['mode'] ?? '') {
2019-06-06 20:09:27 +00:00
case 'display':
2020-06-04 18:48:01 +00:00
$currentUser->setDisplayRole($roleInfo);
2019-06-06 20:09:27 +00:00
break;
case 'leave':
2020-06-04 18:48:01 +00:00
if($roleInfo->getCanLeave())
$currentUser->removeRole($roleInfo);
else
2019-06-06 20:09:27 +00:00
$errors[] = "You're not allow to leave this role, an administrator has to remove it for you.";
break;
}
}
}
if($isVerifiedRequest && isset($_POST['tfa']['enable']) && (bool)$twoFactorInfo['totp_enabled'] !== (bool)$_POST['tfa']['enable']) {
if((bool)$_POST['tfa']['enable']) {
2019-12-09 03:24:10 +00:00
$tfaKey = TOTP::generateKey();
2020-05-28 17:52:31 +00:00
$tfaIssuer = Config::get('site.name', Config::TYPE_STR, 'Misuzu');
2019-12-09 03:24:10 +00:00
$tfaQrcode = (new QRCode(new QROptions([
'version' => 5,
'outputType' => QRCode::OUTPUT_IMAGE_JPG,
'eccLevel' => QRCode::ECC_L,
2020-05-28 17:52:31 +00:00
])))->render(sprintf('otpauth://totp/%s:%s?%s', $tfaIssuer, $twoFactorInfo['username'], http_build_query([
2019-12-09 03:24:10 +00:00
'secret' => $tfaKey,
2020-05-28 17:52:31 +00:00
'issuer' => $tfaIssuer,
2019-12-09 03:24:10 +00:00
])));
2019-06-06 20:09:27 +00:00
Template::set([
2019-06-06 20:09:27 +00:00
'settings_2fa_code' => $tfaKey,
2019-12-09 03:24:10 +00:00
'settings_2fa_image' => $tfaQrcode,
2019-06-06 20:09:27 +00:00
]);
user_totp_update($currentUserId, $tfaKey);
} else {
user_totp_update($currentUserId, null);
}
$twoFactorInfo['totp_enabled'] = !$twoFactorInfo['totp_enabled'];
}
if($isVerifiedRequest && !empty($_POST['current_password'])) {
if(!$currentUser->checkPassword($_POST['current_password'] ?? '')) {
2019-06-06 20:09:27 +00:00
$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($currentUser->getEMailAddress() === mb_strtolower($_POST['email']['confirm'])) {
2019-06-06 20:09:27 +00:00
$errors[] = 'This is already your e-mail address!';
} else {
$checkMail = User::validateEMailAddress($_POST['email']['new'], true);
2019-06-06 20:09:27 +00:00
2019-06-10 17:04:53 +00:00
if($checkMail !== '') {
switch($checkMail) {
2019-06-06 20:09:27 +00:00
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']);
2020-05-21 15:05:30 +00:00
AuditLog::create(AuditLog::PERSONAL_EMAIL_CHANGE, [
2019-06-06 20:09:27 +00:00
$_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::validatePassword($_POST['password']['new']);
2019-06-06 20:09:27 +00:00
if($checkPassword !== '') {
$errors[] = 'The given passwords was too weak.';
} else {
$currentUser->setPassword($_POST['password']['new']);
2020-05-21 15:05:30 +00:00
AuditLog::create(AuditLog::PERSONAL_PASSWORD_CHANGE);
2019-06-06 20:09:27 +00:00
}
}
}
}
}
Template::render('settings.account', [
2019-06-06 20:09:27 +00:00
'errors' => $errors,
2020-06-04 18:48:01 +00:00
'settings_user' => $currentUser,
2019-06-06 20:09:27 +00:00
'is_restricted' => $isRestricted,
'settings_2fa_enabled' => $twoFactorInfo['totp_enabled'],
]);