2024-02-09 12:15:09 +08:00
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace app\controllers;
|
|
|
|
|
|
|
|
|
|
use app\models\User;
|
2024-03-02 19:16:38 +08:00
|
|
|
|
use app\utils\FileSizeHelper;
|
2024-03-05 16:54:24 +08:00
|
|
|
|
use OTPHP\TOTP;
|
2024-02-27 15:41:25 +08:00
|
|
|
|
use ReCaptcha\ReCaptcha;
|
2024-02-09 12:15:09 +08:00
|
|
|
|
use Yii;
|
2024-02-09 12:47:48 +08:00
|
|
|
|
use yii\base\Exception;
|
2024-02-27 15:41:25 +08:00
|
|
|
|
use yii\base\InvalidConfigException;
|
2024-03-05 16:54:24 +08:00
|
|
|
|
use yii\filters\AccessControl;
|
2024-02-27 15:41:25 +08:00
|
|
|
|
use yii\httpclient\Client;
|
2024-02-09 12:15:09 +08:00
|
|
|
|
use yii\web\Controller;
|
|
|
|
|
use yii\web\NotFoundHttpException;
|
|
|
|
|
use yii\filters\VerbFilter;
|
2024-02-09 12:47:48 +08:00
|
|
|
|
use yii\web\Response;
|
2024-02-09 12:15:09 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* UserController implements the CRUD actions for User model.
|
|
|
|
|
*/
|
|
|
|
|
class UserController extends Controller
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* @inheritDoc
|
|
|
|
|
*/
|
2024-03-05 16:54:24 +08:00
|
|
|
|
public function behaviors(): array
|
2024-02-09 12:15:09 +08:00
|
|
|
|
{
|
|
|
|
|
return array_merge(
|
|
|
|
|
parent::behaviors(),
|
|
|
|
|
[
|
2024-03-05 16:54:24 +08:00
|
|
|
|
'access' => [
|
|
|
|
|
'class' => AccessControl::class,
|
|
|
|
|
'rules' => [
|
|
|
|
|
[
|
|
|
|
|
'allow' => true,
|
|
|
|
|
'actions' => ['delete', 'info'],
|
|
|
|
|
'roles' => ['user'],
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
'allow' => true,
|
|
|
|
|
'actions' => ['login', 'register'],
|
|
|
|
|
'roles' => ['?', '@'], // everyone can access public share
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
'allow' => true,
|
|
|
|
|
'actions' => ['logout', 'setup-two-factor', 'change-password'],
|
|
|
|
|
'roles' => ['@'], // everyone can access public share
|
|
|
|
|
]
|
|
|
|
|
],
|
|
|
|
|
],
|
2024-02-09 12:15:09 +08:00
|
|
|
|
'verbs' => [
|
2024-03-05 16:54:24 +08:00
|
|
|
|
'class' => VerbFilter::class,
|
2024-02-09 12:15:09 +08:00
|
|
|
|
'actions' => [
|
2024-03-05 16:54:24 +08:00
|
|
|
|
'login' => ['GET', 'POST'],
|
|
|
|
|
'logout' => ['GET', 'POST'],
|
|
|
|
|
'register' => ['GET', 'POST'],
|
2024-02-09 12:15:09 +08:00
|
|
|
|
'delete' => ['POST'],
|
2024-03-05 16:54:24 +08:00
|
|
|
|
'info' => ['GET', 'POST'],
|
2024-03-03 15:32:49 +08:00
|
|
|
|
'change-password' => ['POST'],
|
2024-03-05 16:54:24 +08:00
|
|
|
|
'setup-two-factor' => ['POST'],
|
2024-02-09 12:15:09 +08:00
|
|
|
|
],
|
|
|
|
|
],
|
|
|
|
|
]
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2024-03-05 16:54:24 +08:00
|
|
|
|
* 删除账户(仅自身)
|
|
|
|
|
* @return Response
|
2024-02-09 12:15:09 +08:00
|
|
|
|
*/
|
2024-03-04 16:51:19 +08:00
|
|
|
|
public function actionDelete(): Response
|
2024-02-09 12:15:09 +08:00
|
|
|
|
{
|
2024-03-04 16:51:19 +08:00
|
|
|
|
$model = Yii::$app->user->identity;
|
|
|
|
|
|
|
|
|
|
if ($model->deleteAccount()) {
|
|
|
|
|
Yii::$app->user->logout();
|
2024-03-05 16:54:24 +08:00
|
|
|
|
Yii::$app->session->setFlash('success', '账户删除成功');
|
|
|
|
|
return $this->redirect(['user/login']);
|
2024-03-04 16:51:19 +08:00
|
|
|
|
} else {
|
2024-03-05 16:54:24 +08:00
|
|
|
|
Yii::$app->session->setFlash('error', '账户删除失败');
|
|
|
|
|
return $this->redirect(['user/info']);
|
2024-03-04 16:51:19 +08:00
|
|
|
|
}
|
2024-02-09 12:15:09 +08:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Finds the User model based on its primary key value.
|
|
|
|
|
* If the model is not found, a 404 HTTP exception will be thrown.
|
|
|
|
|
* @param int $id ID
|
|
|
|
|
* @return User the loaded model
|
|
|
|
|
* @throws NotFoundHttpException if the model cannot be found
|
|
|
|
|
*/
|
2024-03-05 16:54:24 +08:00
|
|
|
|
protected function findModel(int $id): User
|
2024-02-09 12:15:09 +08:00
|
|
|
|
{
|
|
|
|
|
if (($model = User::findOne(['id' => $id])) !== null) {
|
|
|
|
|
return $model;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
throw new NotFoundHttpException('The requested page does not exist.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Displays the login page.
|
2024-02-09 12:47:48 +08:00
|
|
|
|
* visit via https://devs.chenx221.cyou:8081/index.php?r=user%2Flogin
|
|
|
|
|
*
|
2024-02-16 13:59:54 +08:00
|
|
|
|
* @return string|Response
|
2024-03-05 16:54:24 +08:00
|
|
|
|
* @throws InvalidConfigException
|
|
|
|
|
* @throws \yii\httpclient\Exception
|
2024-02-09 12:15:09 +08:00
|
|
|
|
*/
|
2024-02-27 15:41:25 +08:00
|
|
|
|
public function actionLogin(): Response|string
|
2024-02-09 12:15:09 +08:00
|
|
|
|
{
|
|
|
|
|
if (!Yii::$app->user->isGuest) {
|
2024-03-05 16:54:24 +08:00
|
|
|
|
Yii::$app->session->setFlash('error', '账户已登录,请不要重复登录');
|
2024-02-09 12:15:09 +08:00
|
|
|
|
return $this->goHome();
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-09 12:47:48 +08:00
|
|
|
|
$model = new User(['scenario' => 'login']);
|
2024-02-09 12:15:09 +08:00
|
|
|
|
|
|
|
|
|
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
|
2024-02-27 15:41:25 +08:00
|
|
|
|
// 根据 verifyProvider 的值选择使用哪种验证码服务
|
|
|
|
|
$verifyProvider = Yii::$app->params['verifyProvider'];
|
|
|
|
|
$captchaResponse = null;
|
|
|
|
|
$isCaptchaValid = false;
|
|
|
|
|
if ($verifyProvider === 'reCAPTCHA') {
|
|
|
|
|
$captchaResponse = Yii::$app->request->post('g-recaptcha-response', null);
|
|
|
|
|
$isCaptchaValid = $this->validateRecaptcha($captchaResponse);
|
|
|
|
|
} elseif ($verifyProvider === 'hCaptcha') {
|
|
|
|
|
$captchaResponse = Yii::$app->request->post('h-captcha-response', null);
|
|
|
|
|
$isCaptchaValid = $this->validateHcaptcha($captchaResponse);
|
|
|
|
|
} elseif ($verifyProvider === 'Turnstile') {
|
|
|
|
|
$captchaResponse = Yii::$app->request->post('cf-turnstile-response', null);
|
|
|
|
|
$isCaptchaValid = $this->validateTurnstile($captchaResponse);
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-28 16:07:39 +08:00
|
|
|
|
if (($captchaResponse !== null && $isCaptchaValid) || ($verifyProvider === 'None')) {
|
2024-02-27 15:41:25 +08:00
|
|
|
|
if ($model->login()) {
|
2024-03-01 18:04:04 +08:00
|
|
|
|
//login success
|
|
|
|
|
$user = Yii::$app->user->identity;
|
|
|
|
|
$user->last_login = date('Y-m-d H:i:s');
|
|
|
|
|
$user->last_login_ip = Yii::$app->request->userIP;
|
|
|
|
|
if ($user->save(false)) {
|
|
|
|
|
return $this->goBack();
|
|
|
|
|
} else {
|
|
|
|
|
Yii::$app->session->setFlash('error', '登陆成功,但出现了内部错误');
|
|
|
|
|
}
|
2024-02-27 15:41:25 +08:00
|
|
|
|
} else {
|
2024-03-04 16:51:19 +08:00
|
|
|
|
Yii::$app->session->setFlash('error', '用户名密码错误或账户已禁用');
|
2024-02-27 15:41:25 +08:00
|
|
|
|
}
|
2024-02-09 12:15:09 +08:00
|
|
|
|
} else {
|
2024-03-04 16:51:19 +08:00
|
|
|
|
Yii::$app->session->setFlash('error', '请等待验证码加载并完成验证');
|
2024-02-09 12:15:09 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return $this->render('login', [
|
|
|
|
|
'model' => $model,
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-27 15:41:25 +08:00
|
|
|
|
/**
|
|
|
|
|
* 验证 reCAPTCHA 的响应
|
|
|
|
|
* 无法保证这项服务在中国大陆的可用性
|
|
|
|
|
* @param $recaptchaResponse
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
private function validateRecaptcha($recaptchaResponse): bool
|
|
|
|
|
{
|
|
|
|
|
$recaptcha = new ReCaptcha(Yii::$app->params['reCAPTCHA']['secret']);
|
|
|
|
|
$resp = $recaptcha->verify($recaptchaResponse, $_SERVER['REMOTE_ADDR']);
|
|
|
|
|
|
|
|
|
|
return $resp->isSuccess();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 验证 hCaptcha 的响应
|
|
|
|
|
* @param $hcaptchaResponse
|
|
|
|
|
* @return bool
|
|
|
|
|
* @throws InvalidConfigException
|
|
|
|
|
* @throws \yii\httpclient\Exception
|
|
|
|
|
*/
|
|
|
|
|
private function validateHcaptcha($hcaptchaResponse): bool
|
|
|
|
|
{
|
|
|
|
|
$hcaptchaSecret = Yii::$app->params['hCaptcha']['secret'];
|
|
|
|
|
$verifyUrl = 'https://api.hcaptcha.com/siteverify';
|
|
|
|
|
|
|
|
|
|
$client = new Client();
|
|
|
|
|
$response = $client->createRequest()
|
|
|
|
|
->setMethod('POST')
|
|
|
|
|
->setUrl($verifyUrl)
|
|
|
|
|
->setData(['secret' => $hcaptchaSecret, 'response' => $hcaptchaResponse])
|
|
|
|
|
->send();
|
|
|
|
|
|
|
|
|
|
if ($response->isOk) {
|
|
|
|
|
$responseData = $response->getData();
|
|
|
|
|
return isset($responseData['success']) && $responseData['success'] === true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 验证 Turnstile 的响应
|
|
|
|
|
* @param $turnstileResponse
|
|
|
|
|
* @return bool
|
|
|
|
|
* @throws InvalidConfigException
|
|
|
|
|
* @throws \yii\httpclient\Exception
|
|
|
|
|
*/
|
|
|
|
|
private function validateTurnstile($turnstileResponse): bool
|
|
|
|
|
{
|
|
|
|
|
$turnstileSecret = Yii::$app->params['Turnstile']['secret'];
|
|
|
|
|
$verifyUrl = 'https://challenges.cloudflare.com/turnstile/v0/siteverify';
|
|
|
|
|
|
|
|
|
|
$client = new Client();
|
|
|
|
|
$response = $client->createRequest()
|
|
|
|
|
->setMethod('POST')
|
|
|
|
|
->setUrl($verifyUrl)
|
|
|
|
|
->setData(['secret' => $turnstileSecret, 'response' => $turnstileResponse])
|
|
|
|
|
->send();
|
|
|
|
|
|
|
|
|
|
if ($response->isOk) {
|
|
|
|
|
$responseData = $response->getData();
|
|
|
|
|
return isset($responseData['success']) && $responseData['success'] === true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-09 12:15:09 +08:00
|
|
|
|
/**
|
|
|
|
|
* Logs out the current user.
|
2024-02-16 13:59:54 +08:00
|
|
|
|
* @return Response
|
2024-02-09 12:15:09 +08:00
|
|
|
|
*/
|
2024-03-05 16:54:24 +08:00
|
|
|
|
public function actionLogout(): Response
|
2024-02-09 12:15:09 +08:00
|
|
|
|
{
|
|
|
|
|
Yii::$app->user->logout();
|
|
|
|
|
return $this->goHome();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Displays the registration page.
|
2024-02-09 12:47:48 +08:00
|
|
|
|
* visit via https://devs.chenx221.cyou:8081/index.php?r=user%2Fregister
|
|
|
|
|
* @return string|Response
|
|
|
|
|
* @throws Exception
|
2024-02-09 12:15:09 +08:00
|
|
|
|
*/
|
2024-02-27 15:41:25 +08:00
|
|
|
|
public function actionRegister(): Response|string
|
2024-02-09 12:15:09 +08:00
|
|
|
|
{
|
2024-03-05 16:54:24 +08:00
|
|
|
|
if (!Yii::$app->user->isGuest) {
|
|
|
|
|
Yii::$app->session->setFlash('error', '账户已登录,无法进行注册操作');
|
|
|
|
|
return $this->goHome();
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-09 12:47:48 +08:00
|
|
|
|
$model = new User(['scenario' => 'register']);
|
2024-02-09 12:15:09 +08:00
|
|
|
|
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
|
2024-02-27 15:41:25 +08:00
|
|
|
|
// 根据 verifyProvider 的值选择使用哪种验证码服务
|
|
|
|
|
$verifyProvider = Yii::$app->params['verifyProvider'];
|
|
|
|
|
$captchaResponse = null;
|
|
|
|
|
$isCaptchaValid = false;
|
|
|
|
|
if ($verifyProvider === 'reCAPTCHA') {
|
|
|
|
|
$captchaResponse = Yii::$app->request->post('g-recaptcha-response', null);
|
|
|
|
|
$isCaptchaValid = $this->validateRecaptcha($captchaResponse);
|
|
|
|
|
} elseif ($verifyProvider === 'hCaptcha') {
|
|
|
|
|
$captchaResponse = Yii::$app->request->post('h-captcha-response', null);
|
|
|
|
|
$isCaptchaValid = $this->validateHcaptcha($captchaResponse);
|
|
|
|
|
} elseif ($verifyProvider === 'Turnstile') {
|
|
|
|
|
$captchaResponse = Yii::$app->request->post('cf-turnstile-response', null);
|
|
|
|
|
$isCaptchaValid = $this->validateTurnstile($captchaResponse);
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-28 16:07:39 +08:00
|
|
|
|
if (($captchaResponse !== null && $isCaptchaValid) || ($verifyProvider === 'None')) {
|
2024-02-27 15:41:25 +08:00
|
|
|
|
$raw_password = $model->password;
|
|
|
|
|
$model->password = Yii::$app->security->generatePasswordHash($raw_password);
|
|
|
|
|
$model->auth_key = Yii::$app->security->generateRandomString();
|
2024-03-01 18:04:04 +08:00
|
|
|
|
$model->created_at = date('Y-m-d H:i:s');
|
|
|
|
|
$model->role = 'user';
|
2024-03-03 14:25:14 +08:00
|
|
|
|
$model->name = $model->username; //用户默认昵称为用户名,后期可以修改
|
2024-02-27 15:41:25 +08:00
|
|
|
|
if ($model->save(false)) { // save without validation
|
2024-03-04 15:51:45 +08:00
|
|
|
|
$userFolder = Yii::getAlias(Yii::$app->params['dataDirectory']) . '/' . $model->id;
|
|
|
|
|
if (!is_dir($userFolder)) {
|
|
|
|
|
mkdir($userFolder);
|
|
|
|
|
}
|
2024-02-27 15:41:25 +08:00
|
|
|
|
Yii::$app->session->setFlash('success', 'Registration successful. You can now log in.');
|
|
|
|
|
return $this->redirect(['login']);
|
|
|
|
|
} else {
|
|
|
|
|
$model->password = $raw_password;
|
|
|
|
|
Yii::$app->session->setFlash('error', 'Failed to register user.');
|
|
|
|
|
}
|
2024-02-09 12:15:09 +08:00
|
|
|
|
} else {
|
2024-02-27 15:41:25 +08:00
|
|
|
|
Yii::$app->session->setFlash('error', 'Invalid captcha.');
|
2024-02-09 12:15:09 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->render('register', [
|
|
|
|
|
'model' => $model,
|
|
|
|
|
]);
|
|
|
|
|
}
|
2024-03-01 18:04:04 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return string|Response
|
|
|
|
|
*/
|
2024-03-03 15:32:49 +08:00
|
|
|
|
public function actionInfo(string $focus = null): Response|string
|
2024-03-01 18:04:04 +08:00
|
|
|
|
{
|
|
|
|
|
$model = Yii::$app->user->identity;
|
2024-03-03 15:32:49 +08:00
|
|
|
|
$usedSpace = FileSizeHelper::getDirectorySize(Yii::getAlias(Yii::$app->params['dataDirectory']) . '/' . Yii::$app->user->id);
|
2024-03-02 19:16:38 +08:00
|
|
|
|
$vaultUsedSpace = 0; // 保险箱已用空间,暂时为0
|
|
|
|
|
$storageLimit = $model->storage_limit;
|
2024-03-05 16:54:24 +08:00
|
|
|
|
$totp_secret = null;
|
|
|
|
|
$totp_url = null;
|
|
|
|
|
if (!$model->is_otp_enabled) {
|
|
|
|
|
$totp = TOTP::generate();
|
|
|
|
|
$totp_secret = $totp->getSecret();
|
|
|
|
|
$totp->setLabel('NetDisk_'.$model->name);
|
|
|
|
|
$totp_url = $totp->getProvisioningUri();
|
|
|
|
|
}
|
2024-03-03 14:25:14 +08:00
|
|
|
|
if (Yii::$app->request->isPost && $model->load(Yii::$app->request->post())) {
|
2024-03-03 15:32:49 +08:00
|
|
|
|
if ($model->save()) {
|
2024-03-03 14:25:14 +08:00
|
|
|
|
Yii::$app->session->setFlash('success', '个人简介已更新');
|
2024-03-03 15:32:49 +08:00
|
|
|
|
return $this->render('info', [
|
|
|
|
|
'model' => $model,
|
|
|
|
|
'usedSpace' => $usedSpace, // B
|
|
|
|
|
'vaultUsedSpace' => $vaultUsedSpace, // B
|
|
|
|
|
'storageLimit' => $storageLimit, // MB
|
|
|
|
|
'focus' => 'bio',
|
2024-03-05 16:54:24 +08:00
|
|
|
|
'totp_secret' => $totp_secret,
|
|
|
|
|
'totp_url' => $totp_url,
|
2024-03-03 15:32:49 +08:00
|
|
|
|
]);
|
2024-03-03 14:25:14 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2024-03-01 18:04:04 +08:00
|
|
|
|
return $this->render('info', [
|
|
|
|
|
'model' => $model,
|
2024-03-02 19:16:38 +08:00
|
|
|
|
'usedSpace' => $usedSpace, // B
|
2024-03-03 13:45:31 +08:00
|
|
|
|
'vaultUsedSpace' => $vaultUsedSpace, // B
|
2024-03-02 19:16:38 +08:00
|
|
|
|
'storageLimit' => $storageLimit, // MB
|
2024-03-03 15:32:49 +08:00
|
|
|
|
'focus' => $focus,
|
2024-03-05 16:54:24 +08:00
|
|
|
|
'totp_secret' => $totp_secret,
|
|
|
|
|
'totp_url' => $totp_url,
|
2024-03-01 18:04:04 +08:00
|
|
|
|
]);
|
|
|
|
|
}
|
2024-03-02 19:16:38 +08:00
|
|
|
|
|
2024-03-03 15:32:49 +08:00
|
|
|
|
/**
|
|
|
|
|
* @return Response|string
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public function actionChangePassword(): Response|string
|
|
|
|
|
{
|
|
|
|
|
$model = Yii::$app->user->identity;
|
|
|
|
|
$model->scenario = 'changePassword';
|
|
|
|
|
|
|
|
|
|
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
|
|
|
|
|
$model->password = Yii::$app->security->generatePasswordHash($model->newPassword);
|
|
|
|
|
if ($model->save(false)) {
|
|
|
|
|
Yii::$app->session->setFlash('success', 'Password changed successfully.');
|
|
|
|
|
} else {
|
|
|
|
|
Yii::$app->session->setFlash('error', 'Failed to change password.');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return $this->redirect(['user/info', 'focus' => 'password']);
|
|
|
|
|
}
|
2024-03-04 15:51:45 +08:00
|
|
|
|
|
2024-03-05 16:54:24 +08:00
|
|
|
|
/**
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public function actionSetupTwoFactor(): string
|
|
|
|
|
{
|
|
|
|
|
$user = Yii::$app->user->identity;
|
|
|
|
|
$totp = TOTP::create();
|
|
|
|
|
$user->otp_secret = $totp->getSecret();
|
|
|
|
|
$user->is_otp_enabled = true;
|
|
|
|
|
$user->save(false);
|
|
|
|
|
|
|
|
|
|
$otpauth = $totp->getProvisioningUri($user->username);
|
|
|
|
|
$qrCodeUrl = 'https://api.qrserver.com/v1/create-qr-code/?data=' . urlencode($otpauth);
|
|
|
|
|
|
|
|
|
|
return $this->render('setup-two-factor', ['qrCodeUrl' => $qrCodeUrl]);
|
|
|
|
|
}
|
2024-03-04 15:51:45 +08:00
|
|
|
|
|
2024-02-09 12:15:09 +08:00
|
|
|
|
}
|