-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
393 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php | ||
|
||
namespace App\Controllers; | ||
|
||
use App\Models\User; | ||
use App\Models\PasswordReset; | ||
use App\Services\Password; | ||
use App\Utils\Hash; | ||
|
||
/*** | ||
* Class Password | ||
* @package App\Controllers | ||
* 密码重置 | ||
*/ | ||
|
||
class PasswordController extends BaseController | ||
{ | ||
public function reset(){ | ||
return $this->view()->display('password/reset.tpl'); | ||
} | ||
|
||
public function handleReset($request, $response, $args){ | ||
$email = $request->getParam('email'); | ||
// check limit | ||
|
||
// send email | ||
$user = User::where('email',$email)->first(); | ||
if ($user == null){ | ||
$rs['ret'] = 0; | ||
$rs['msg'] = '此邮箱不存在.'; | ||
return $response->getBody()->write(json_encode($rs)); | ||
} | ||
Password::sendResetEmail($email); | ||
$rs['ret'] = 1; | ||
$rs['msg'] = '重置邮件已经发送,请检查邮箱.'; | ||
return $response->getBody()->write(json_encode($rs)); | ||
} | ||
|
||
public function token($request, $response, $args){ | ||
$token = $args['token']; | ||
return $this->view()->assign('token',$token)->display('password/token.tpl'); | ||
} | ||
|
||
public function handleToken($request, $response, $args){ | ||
$tokenStr = $args['token']; | ||
$password = $request->getParam('password'); | ||
// check token | ||
$token = PasswordReset::where('token',$tokenStr)->first(); | ||
if ($token == null || $token->expire_time < time() ){ | ||
$rs['ret'] = 0; | ||
$rs['msg'] = '链接已经失效,请重新获取'; | ||
return $response->getBody()->write(json_encode($rs)); | ||
} | ||
|
||
$user = User::where('email',$token->email)->first(); | ||
if ($user == null){ | ||
$rs['ret'] = 0; | ||
$rs['msg'] = '链接已经失效,请重新获取'; | ||
return $response->getBody()->write(json_encode($rs)); | ||
} | ||
|
||
// reset password | ||
$hashPassword = Hash::passwordHash($password); | ||
$user->pass = $hashPassword; | ||
if(!$user->save()){ | ||
$rs['ret'] = 0; | ||
$rs['msg'] = '重置失败,请重试'; | ||
return $response->getBody()->write(json_encode($rs)); | ||
} | ||
$rs['ret'] = 1; | ||
$rs['msg'] = '重置成功'; | ||
return $response->getBody()->write(json_encode($rs)); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
namespace App\Models; | ||
|
||
|
||
class PasswordReset extends Model | ||
{ | ||
protected $table = 'ss_password_reset'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
|
||
namespace App\Services; | ||
|
||
use App\Models\PasswordReset; | ||
use App\Utils\Tools; | ||
/*** | ||
* Class Password | ||
* @package App\Services | ||
*/ | ||
|
||
class Password | ||
{ | ||
/** | ||
* @param $email string | ||
* @return bool | ||
*/ | ||
public static function sendResetEmail($email){ | ||
$pwdRst = new PasswordReset(); | ||
$pwdRst->email = $email; | ||
$pwdRst->init_time = time(); | ||
$pwdRst->expire_time = time() + 3600*24; // @todo | ||
$pwdRst->token = Tools::genRandomChar(64); | ||
if(!$pwdRst->save()){ | ||
return false; | ||
} | ||
$subject = Config::get('appName')."重置密码"; | ||
$text = '请访问此链接申请重置密码'.Config::get('baseUrl')."/password/token/"; | ||
try{ | ||
Mail::send($email,$subject,$text); | ||
}catch (Exception $e){ | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
public static function resetBy($token,$password){ | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.