/** * @return \Zend\Http\Response|ViewModel */ public function indexAction() { $user = $this->getUserService()->getUserMapper()->findFirst(); if ($user) { return $this->redirect()->toRoute('mfcc-admin-dashboard'); } $form = new UserForm(UserForm::STATUS_INSTALL); $form->setInputFilter(new UserInputFilter()); if ($this->getRequest()->isPost()) { $post = $this->getRequest()->getPost()->toArray(); $form->setData($post); if ($form->isValid()) { try { $userService = $this->getUserService(); $userService->createSuperuser($post); $this->flashMessenger()->addSuccessMessage('User has been successfully added'); return $this->redirect()->toRoute('mfcc-admin-user/login'); } catch (\Exception $e) { $this->flashMessenger()->addErrorMessage('Error: ' . $e->getMessage()); return $this->redirect()->toRoute('mfcc-admin-install'); } } } $this->layout('layout/admin-login'); return new ViewModel(['form' => $form]); }
/** * @return \Zend\Http\Response|ViewModel */ public function updateAction() { if (!$this->acl()->isAllowed('users', AccessControl::WRITE)) { return $this->redirect()->toRoute('mfcc-admin-dashboard'); } $id = (int) $this->params()->fromRoute('id', 0); if (!$id) { return $this->redirect()->toRoute('mfcc-admin-users'); } $userService = $this->getUserMapper(); $user = $userService->findById($id); if (!$user) { return $this->redirect()->toRoute('mfcc-admin-users'); } $form = new UserForm(UserForm::STATUS_UPDATE); $form->setInputFilter(new UserInputFilter()); $roles = $this->config['mfcc_admin']['roles']; $rolesData = ['admin' => 'Admin']; foreach ($roles as $roleId => $role) { $rolesData[$roleId] = $role['name']; } $form->get('roleId')->setValueOptions($rolesData); $form->populateValues($user->toArray()); if ($this->getRequest()->isPost()) { $post = $this->getRequest()->getPost()->toArray(); $form->setData($post); if ($form->isValid()) { try { $userService = $this->getUserService(); $userService->update($user, $post, $this->userAuthentication()->getIdentity()); $this->flashMessenger()->addSuccessMessage('User has been successfully updated.'); } catch (\Exception $e) { $this->flashMessenger()->addErrorMessage('Error: ' . $e->getMessage()); } return $this->redirect()->toRoute('mfcc-admin-users/update', ['id' => $id]); } } return new ViewModel(['form' => $form, 'user' => $user]); }