/**
  * @Request({"user": "******"}, csrf=true)
  */
 public function saveAction($data)
 {
     if (!$this->user->isAuthenticated()) {
         $this->getApplication()->abort(404);
     }
     try {
         $user = $this->users->find($this->user->getId());
         $name = trim(@$data['name']);
         $email = trim(@$data['email']);
         $passNew = @$data['password_new'];
         $passOld = @$data['password_old'];
         if (strlen($name) < 3) {
             throw new Exception(__('Name is invalid.'));
         }
         if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
             throw new Exception(__('Email is invalid.'));
         }
         if ($this->users->where(['email = ?', 'id <> ?'], [$email, $user->getId()])->first()) {
             throw new Exception(__('Email not available.'));
         }
         if ($passNew) {
             if (!$this['auth']->getUserProvider()->validateCredentials($this->user, ['password' => $passOld])) {
                 throw new Exception(__('Invalid Password.'));
             }
             if (trim($passNew) != $passNew || strlen($passNew) < 3) {
                 throw new Exception(__('New Password is invalid.'));
             }
             $user->setPassword($this['auth.password']->hash($passNew));
         }
         if ($email != $user->getEmail()) {
             $user->set('verified', false);
         }
         $user->setName($name);
         $user->setEmail($email);
         $this['events']->dispatch('system.user.profile.save', new ProfileSaveEvent($user, $data));
         $this->users->save($user);
         $this['events']->dispatch('system.user.profile.saved', new ProfileSaveEvent($user, $data));
         $this['message']->success(__('Profile updated.'));
     } catch (Exception $e) {
         $this['message']->error($e->getMessage());
     }
     return $this->redirect('@system/profile');
 }
示例#2
0
 /**
  * @Request({"status": "int", "ids": "int[]"}, csrf=true)
  * @Response("json")
  */
 public function statusAction($status, $ids = [])
 {
     if ($status == User::STATUS_BLOCKED && in_array($this->user->getId(), $ids)) {
         return ['message' => __('Unable to block yourself.'), 'error' => true];
     }
     foreach ($ids as $id) {
         if ($user = $this->users->find($id)) {
             $user->setActivation('');
             if ($status != $user->getStatus()) {
                 $this->users->save($user, compact('status'));
             }
         }
     }
     if ($status == User::STATUS_BLOCKED) {
         $message = _c('{1} User blocked.|]1,Inf[ Users blocked.', count($ids));
     } else {
         $message = _c('{1} User activated.|]1,Inf[ Users activated.', count($ids));
     }
     return ['message' => $message];
 }