Esempio n. 1
0
 public function add()
 {
     $this->setTitle('Add User');
     $this->addBreadcrumb('Add User', '/user/add');
     if ($this->request->getMethod() == 'POST') {
         $form = $this->userForm($this->getParams());
         if ($form->validate()) {
             if ($this->userStore->getByEmail($this->getParam('email'))) {
                 $error = 'This email address already belongs to a registered user.';
                 $form->getChild('fieldset')->getChild('email')->setError($error);
                 $this->view->form = $form->render();
                 return;
             }
             try {
                 $user = new User();
                 $params = $this->getParams();
                 $params['hash'] = password_hash($params['password'], PASSWORD_DEFAULT);
                 $user->setValues($params);
                 $user->setDateAdded(new \DateTime());
                 $user = $this->userStore->save($user);
                 $data = [$user, $params];
                 Event::trigger('userSaved', $data);
                 list($user, $params) = $data;
                 $permission = new Permission();
                 $permission->setUserId($user->getId());
                 $permission->setCanAccess(true);
                 $permission->setUri('/');
                 $this->permissionStore->save($permission);
                 $this->successMessage($params['name'] . ' was added successfully.', true);
                 $this->redirect('/user');
             } catch (Exception $e) {
                 $this->errorMessage('There was an error adding the user. Please try again.');
             }
         } else {
             $this->errorMessage('There was an error adding the user. Please try again.');
         }
         $this->view->form = $form->render();
     } else {
         $this->view->form = $this->userForm(array())->render();
     }
 }