diff --git a/app/assets/sass/components/_badge.scss b/app/assets/sass/components/_badge.scss index 254e560..794b1f6 100644 --- a/app/assets/sass/components/_badge.scss +++ b/app/assets/sass/components/_badge.scss @@ -11,8 +11,36 @@ display: none; } + // Contextual colors + // ---------------------------------- + + &-default { + color: white; + background-color: $gray-400; + } + &-primary { color: white; background-color: $primary; } + + &-info { + color: white; + background-color: $info; + } + + &-success { + color: white; + background-color: $success; + } + + &-warning { + color: black; + background-color: $warning; + } + + &-danger { + color: white; + background-color: $danger; + } } diff --git a/app/config/routes.yml b/app/config/routes.yml index 5510b37..71b1d67 100644 --- a/app/config/routes.yml +++ b/app/config/routes.yml @@ -62,8 +62,20 @@ router: pattern: '/admin' path: backend::user::index backend-user-list: - pattern: '/admin/user/list/{page:([0-9]+)}' + pattern: '/admin/user/list{page:/?([0-9]+)?}' path: backend::user::index + backend-user-new: + pattern: '/admin/user/new' + path: backend::user::new + backend-user-edit: + pattern: '/admin/user/{id:([0-9]+)}' + path: backend::user::edit + backend-user-status: + pattern: '/admin/user/{id:([0-9]+)}/{type}' + path: + module: backend + controller: user + action: status backend-log: pattern: '/admin/log{page:/?([0-9]+)?}' path: backend::log::index diff --git a/app/controllers/backend/UserController.php b/app/controllers/backend/UserController.php index 0bea10c..c1f8206 100644 --- a/app/controllers/backend/UserController.php +++ b/app/controllers/backend/UserController.php @@ -2,7 +2,8 @@ namespace App\Controller\Backend; -use App\Model\Data\User; +use App\Model\Data\User, + App\Form\UserSettings as UserSettingsForm; class UserController extends \Phalcon\Mvc\Controller { @@ -21,4 +22,79 @@ class UserController extends \Phalcon\Mvc\Controller $this->view->pagination_url = '/admin/user/list/'; $this->view->page = $paginator->paginate(); } + + public function newAction() + { + $user = new User; + $form = new UserSettingsForm($user, true); + + if ($this->request->isPost()) { + $data = $this->request->getPost(); + if ($form->isValid($data)) { + + $new_pw = $form->getValue('passwordNew'); + if (strlen($new_pw) > 0) { + $hash = $this->security->hash($new_pw, 12); + $user->setPassword($hash); + } + + $form->initialize(); + + $this->flash->message('success', 'User created!'); + $this->response->redirect(['for' => 'backend-user-list']); + return; + } + + $this->flash->message('error', 'Could not create user'); + } + + $this->view->user = $user; + $this->view->form = $form; + $this->view->pick('user/form'); + } + + public function editAction($id) + { + $user = User::findFirstById($id); + $form = new UserSettingsForm($user, true); + + if ($this->request->isPost()) { + $data = $this->request->getPost(); + + if ($form->isValid($data)) { + + $new_pw = $form->getValue('passwordNew'); + if (strlen($new_pw) > 0) { + $hash = $this->security->hash($new_pw, 12); + $user->setPassword($hash); + } + $user->save(); + $form->initialize(); + + $this->flash->message('success', 'User saved!'); + } else { + $this->flash->message('error', 'Could not save user'); + } + } + + $this->view->user = $user; + $this->view->form = $form; + $this->view->pick('user/form'); + } + + public function statusAction($id, $type) + { + $user = User::findFirstById($id); + $user->setStatus(ucfirst($type)); + $user->save(); + + $status = $user->getStatus(); + // Bit of a hack to convert "active" to verb. + if ($status === User::STATUS_ACTIVE) { + $status = 'Activated'; + } + + $this->flash->success('The account was: ' . $status); + $this->response->redirect('/admin'); + } } diff --git a/app/forms/UserSettings.php b/app/forms/UserSettings.php index 3e6387e..4eae39f 100644 --- a/app/forms/UserSettings.php +++ b/app/forms/UserSettings.php @@ -35,23 +35,41 @@ use Phalcon\Validation\Validator\Callback as CallbackValidator, class UserSettings extends FormBase { + /** + * @var bool + */ + protected $_admin; + + public function __construct(UserModel $user = null, bool $admin = false) + { + $this->_admin = $admin; + + parent::__construct($user); + } + public function initialize() { + $entity = $this->getEntity(); + $this->setValidation(new \Phalcon\Validation()); // Id - $id = new Text('id', array( - 'class' => 'form-control', - 'readonly' => '', - 'disabled' => 'disabled', - )); - $id->addValidator(new IdenticalValidator([ - 'accepted' => $this->getEntity()->getId(), - 'allowEmpty' => true - ])); + if ($entity && $entity->getId()) { - $id->setLabel('ID'); - $this->add($id); + $id = new Text('id', array( + 'class' => 'form-control', + 'readonly' => '', + 'disabled' => 'disabled', + )); + + $id->addValidator(new IdenticalValidator([ + 'accepted' => $entity->getId(), + 'allowEmpty' => true + ])); + + $id->setLabel('ID'); + $this->add($id); + } // Username $username = new Text('username', array( @@ -61,16 +79,20 @@ class UserSettings extends FormBase $username->setLabel('Username'); - $username->addValidator(new AlnumValidator()); - - $validator = new UniquenessValidator(array( + $validator_options = array( 'model' => new UserModel(), - 'message' => 'The username already exists.', + 'message' => 'The :field already exists.', 'attribute' => 'username', - 'except' => [ $this->getEntity()->getUsername() ] - )); + ); - $username->addValidator($validator); + if ($entity && strlen($entity->getUsername())) { + $validator_options['except'] = [ $entity->getUsername() ]; + } + + $username->addValidators([ + new AlnumValidator(), + new UniquenessValidator($validator_options) + ]); $this->add($username); @@ -89,27 +111,48 @@ class UserSettings extends FormBase $this->add($name); // Email - $email = new Text('email', array( - 'class' => 'form-control', - 'placeholder' => 'Email', - 'readonly' => '', - 'disabled' => 'disabled', - )); + if ($this->_admin === false && $entity) { + $email = new Text('email', array( + 'class' => 'form-control', + 'placeholder' => 'Email', + 'readonly' => '', + 'disabled' => 'disabled', + )); - $email->addValidator(new IdenticalValidator([ - 'accepted' => $this->getEntity()->getEmail(), - 'allowEmpty' => true - ])); + $email->addValidator(new IdenticalValidator([ + 'accepted' => $entity->getEmail(), + 'allowEmpty' => true + ])); + } else { + $email = new Text('email', array( + 'class' => 'form-control', + 'placeholder' => 'Email', + )); + + $validator_options = [ + 'model' => new UserModel(), + 'message' => 'The :field already exists.', + 'attribute' => 'email', + ]; + + if ($entity && strlen($entity->getEmail())) { + $validator_options['except'] = [ $entity->getEmail() ]; + } + + $email->addValidators([ + new EmailValidator(), + new UniquenessValidator($validator_options) + ]); + } $email->setLabel('Email'); - $this->add($email); // Passwords $this->_passwords(); // Submit - $submit = new Submit('Save', array('class' => 'button button-default')); + $submit = new Submit($entity && $entity->getId() ? 'Save' : 'Create', array('class' => 'button button-default')); $this->add($submit); } @@ -118,10 +161,10 @@ class UserSettings extends FormBase */ protected function _passwords() { - $current_pw = $this->getEntity()->getPassword(); + $entity = $this->getEntity(); // Current - if (strlen($current_pw) > 0) { + if ($this->_admin === false && $entity && strlen($entity->getPassword()) > 0) { $current = new Password('passwordCurrent', array( 'class' => 'form-control', )); @@ -146,7 +189,7 @@ class UserSettings extends FormBase // Validation $validation = $this->getValidation(); - if (strlen($current_pw) > 0) { + if ($this->_admin === false && $entity && strlen($entity->getPassword()) > 0) { $validation->add('passwordCurrent', new CallbackValidator([ 'callback' => function($data) { $new_pw = $data['passwordNew']; diff --git a/app/models/Data/User.php b/app/models/Data/User.php index 122ba68..1fe205c 100644 --- a/app/models/Data/User.php +++ b/app/models/Data/User.php @@ -255,6 +255,16 @@ class User extends Base return $this; } + /** + * Returns true if this is a active user. + * + * @return bool + */ + public function isActive() + { + return $this->status == self::STATUS_ACTIVE; + } + /** * @return mixed */ diff --git a/app/views/backend/user/form.volt b/app/views/backend/user/form.volt new file mode 100644 index 0000000..43d0542 --- /dev/null +++ b/app/views/backend/user/form.volt @@ -0,0 +1,74 @@ + +
{{ user.getStatus() }}
+{% endif %} + +| # | Username | Name | @@ -17,11 +26,16 @@|||||
|---|---|---|---|---|---|---|---|
| + + {{ icon('solid/pen') }} + + | {{ item.id }} | {{ item.username }} | {{ item.name }} | {{ item.email }} | -{{ item.type }} | +{{ item.type | capitalize }} | {{ item.status }} |