public function __construct() { if (isset($_SESSION['user'])) { self::$storage = json_decode($_SESSION['user']); } else { if (isset($_COOKIE['sesskey'])) { $sess = \Model\UserSession::find_by_sesskey($_COOKIE['sesskey']); if ($sess) { $user = \Model\User::find_by_id($sess->userid); self::set($user->to_json()); } } } }
function edit($id) { // user_id $app = get_app(); $user = User::find_by_id($id); // GEt if ($app->request()->isGet()) { render_with_layout('misc.php', 'Users/edit.php', compact('user')); } // POST if ($app->request()->isPost()) { $post = $app->request()->post(); // $user->account = $post['account']; $user->username = $post['username']; $user->group_id = $post['group_id']; $user->role_id = $post['role_id']; // $password_1 = $post['password_1']; $password_2 = $post['password_2']; // 当满足两次输入密码相同 且 密码长度大于8位 // 才更新密码 if ($password_1 == $password_2 && strlen($password_1) >= 8) { $user->password = md5($password_1); } else { $app->flashNow('errors', array('用户密码必须不小于8位!')); render_with_layout('misc.php', 'Users/edit.php', compact('user')); exit; } if (!$user->is_valid()) { $app->flashNow('errors', $user->errrors); render_with_layout('misc.php', 'Users/edit.php', compact('user')); } else { $user->save(); $app->flash('success', '用户信息更新成功!'); redirect('/users'); } } }
function profile() { $app = get_app(); $user = User::find_by_id($_SESSION['user_id']); //GET if ($app->request()->isGet()) { render_with_layout('misc.php', 'Misc/profile.php'); } //POST if ($app->request()->isPost()) { $post = $app->request()->post(); $old_pwd = $post['password']; $new_pwd_1 = $post['new_pwd_1']; $new_pwd_2 = $post['new_pwd_2']; if (empty($old_pwd)) { $app->flashNow('errors', array('当前密码不能为空!')); //render_with_layout('misc.php','Staffs/profile.php'); } else { if (empty($new_pwd_1)) { $app->flashNow('errors', array('新密码不能为空!')); } else { if ($new_pwd_1 != $new_pwd_2) { $app->flashNow('errors', array('两次输入的密码不一致!')); } else { // set password if (md5($old_pwd) != $user->password) { $app->flashNow('errors', array('当前密码错误,密码更新失败!')); } else { $user->password = md5($new_pwd_1); $user->save(); $app->flashNow('success', '密码更新成功!'); } } } } render_with_layout('misc.php', 'Misc/profile.php'); } }