public function __construct()
 {
     // 判断浏览器名称和版本
     $agent = Request::header('user-agent');
     $browser = '';
     $browser_ver = '';
     if (preg_match('/MSIE\\s([^\\s|;]+)/i', $agent, $regs)) {
         $browser = 'Internet Explorer';
         $browser_ver = $regs[1];
     } elseif (preg_match('/FireFox\\/([^\\s]+)/i', $agent, $regs)) {
         $browser = 'FireFox';
         $browser_ver = $regs[1];
     } elseif (preg_match('/Maxthon/i', $agent, $regs)) {
         $browser = '(Internet Explorer ' . $browser_ver . ') Maxthon';
         $browser_ver = '';
     } elseif (preg_match('/Opera[\\s|\\/]([^\\s]+)/i', $agent, $regs)) {
         $browser = 'Opera';
         $browser_ver = $regs[1];
     } elseif (preg_match('/OmniWeb\\/(v*)([^\\s|;]+)/i', $agent, $regs)) {
         $browser = 'OmniWeb';
         $browser_ver = $regs[2];
     } elseif (preg_match('/Netscape([\\d]*)\\/([^\\s]+)/i', $agent, $regs)) {
         $browser = 'Netscape';
         $browser_ver = $regs[2];
     } elseif (preg_match('/safari\\/([^\\s]+)/i', $agent, $regs)) {
         $browser = 'Safari';
         $browser_ver = $regs[1];
     } elseif (preg_match('/NetCaptor\\s([^\\s|;]+)/i', $agent, $regs)) {
         $browser = '(Internet Explorer ' . $browser_ver . ') NetCaptor';
         $browser_ver = $regs[1];
     } elseif (preg_match('/Lynx\\/([^\\s]+)/i', $agent, $regs)) {
         $browser = 'Lynx';
         $browser_ver = $regs[1];
     }
     if ($browser == 'Internet Explorer' && $browser_ver <= 8) {
         if (!Request::ajax()) {
             throw new BrowserNotSupportedException('浏览器不兼容');
         }
     }
     if (!Config::get('constants.installed') && !Request::is('install/*') && !Request::is('api/install')) {
         if (!Request::ajax()) {
             throw new AppNeedInstallException('应用未初始化');
         }
     }
     if (Auth::check()) {
         $this->CurrentUser = Auth::user();
         $this->CurrentUser->updateAct();
     } else {
         $this->CurrentUser = new AmaotoUser();
     }
     View::share('CurrentUser', $this->CurrentUser);
 }
 public function showEditUserPage($userId)
 {
     if (!$this->CurrentUser || !$this->CurrentUser->isAdmin()) {
         return Redirect::to('login');
     }
     $thatUser = AmaotoUser::whereId($userId)->first();
     return View::make('admin/edit-user', array('thatUser' => $thatUser));
 }
 public function doEditUser()
 {
     try {
         // 检查必需参数
         if (!Input::has('id')) {
             throw new InvalidArgumentException('缺少参数');
         }
         // 检查id格式
         $id = Input::get('id');
         $validator = Validator::make(array('ID' => $id), array('ID' => 'required|integer|exists:users,id'));
         if ($validator->fails()) {
             throw new InvalidArgumentException($validator->messages()->first());
         }
         // 获取用户
         $thatUser = AmaotoUser::whereId($id)->first();
         // 判断是否成功获取
         if (!$thatUser) {
             throw new NotExistException('该用户不存在');
         }
         // 检查权限
         if ($this->CurrentUser->id !== $thatUser->id && !$this->CurrentUser->isAdmin()) {
             throw new PermissionDeniedException('无权编辑该用户');
         }
         // username
         if (Input::has('username')) {
             $username = Input::get('username');
             $validator = Validator::make(array('用户名' => $username), array('用户名' => 'required|alpha_dash'));
             if ($validator->fails()) {
                 throw new InvalidArgumentException($validator->messages()->first());
             }
             $thatUser->username = $username;
         }
         // email
         if (Input::has('email')) {
             $email = Input::get('email');
             $validator = Validator::make(array('Email' => $email), array('Email' => 'email'));
             if ($validator->fails()) {
                 throw new InvalidArgumentException($validator->messages()->first());
             }
             $thatUser->email = $email;
         }
         // password
         if (Input::has('password')) {
             $password = Input::get('password');
             $validator = Validator::make(array('密码' => $password), array('密码' => 'min:8'));
             if ($validator->fails()) {
                 throw new InvalidArgumentException($validator->messages()->first());
             }
             if (!strlen($password) == 0) {
                 $thatUser->password = Hash::make($password);
             }
         }
         // password
         if (Input::has('power') && $this->CurrentUser->isAdmin()) {
             $power = Input::get('power');
             $validator = Validator::make(array('权限' => $power), array('权限' => 'required|integer|min:0|max:99999'));
             if ($validator->fails()) {
                 throw new InvalidArgumentException($validator->messages()->first());
             }
             $thatUser->power = $power;
         }
         $thatUser->save();
         return Response::json(array('type' => 'success', 'message' => '修改成功'));
     } catch (InvalidArgumentException $e) {
         return Response::json(array('type' => 'warning', 'message' => $e->getMessage()));
     } catch (Exception $e) {
         return Response::json(array('type' => 'error', 'message' => $e->getMessage()));
     }
 }