From 2121e01f393f49970cb546bf700ab96f8cea0000 Mon Sep 17 00:00:00 2001 From: Hossein Azizabadi Farahani Date: Wed, 27 Jan 2021 11:55:55 +0330 Subject: [PATCH] Add password edit page on admin side --- usr/module/user/config/module.php | 2 +- usr/module/user/config/user.php | 4 +- .../src/Controller/Admin/EditController.php | 118 +++++++++++++++--- .../src/Controller/Admin/ViewController.php | 16 ++- .../user/src/Form/EditPasswordFilter.php | 61 +++++++++ usr/module/user/src/Form/EditPasswordForm.php | 77 ++++++++++++ .../user/template/admin/edit-user.phtml | 16 +-- 7 files changed, 262 insertions(+), 32 deletions(-) create mode 100644 usr/module/user/src/Form/EditPasswordFilter.php create mode 100644 usr/module/user/src/Form/EditPasswordForm.php diff --git a/usr/module/user/config/module.php b/usr/module/user/config/module.php index 5207402e8d..73350d8136 100644 --- a/usr/module/user/config/module.php +++ b/usr/module/user/config/module.php @@ -16,7 +16,7 @@ 'meta' => [ 'title' => _a('User'), 'description' => _a('User profile and services.'), - 'version' => '1.8.1', + 'version' => '1.8.2', 'license' => 'New BSD', 'demo' => 'http://demo.piengine.org', 'icon' => 'fa-user', diff --git a/usr/module/user/config/user.php b/usr/module/user/config/user.php index fbd90fe025..5befc0c1d1 100755 --- a/usr/module/user/config/user.php +++ b/usr/module/user/config/user.php @@ -291,13 +291,13 @@ 'edit' => 'locale', ], - // Profile: mobile phone number + // Profile: mobile number 'mobile' => [ 'name' => 'mobile', 'title' => _a('Mobile phone'), ], - // Profile: phone + // Profile: phone number 'phone' => [ 'name' => 'phone', 'title' => _a('Telephone'), diff --git a/usr/module/user/src/Controller/Admin/EditController.php b/usr/module/user/src/Controller/Admin/EditController.php index 9c67fe151e..85eb04ca7e 100755 --- a/usr/module/user/src/Controller/Admin/EditController.php +++ b/usr/module/user/src/Controller/Admin/EditController.php @@ -13,6 +13,8 @@ use Module\User\Form\CompoundForm; use Module\User\Form\EditUserFilter; use Module\User\Form\EditUserForm; +use Module\User\Form\EditPasswordFilter; +use Module\User\Form\EditPasswordForm; use Pi; use Pi\Mvc\Controller\ActionController; @@ -20,6 +22,7 @@ * Edit user controller * * @author Liu Chuang + * @author Hossein Azizabadi */ class EditController extends ActionController { @@ -31,7 +34,7 @@ public function indexAction() $user = $this->getUser($uid); // Get available edit fields - list($fields, $formFields, $formFilters) = $this->getEditField(); + [$fields, $formFields, $formFilters] = $this->getEditField(); // Add other elements $formFields[] = [ @@ -61,11 +64,7 @@ public function indexAction() // Update user $values = $form->getData(); $values['last_modified'] = time(); - if (isset($values['credential']) - && !$values['credential'] - ) { - unset($values['credential']); - } + unset($values['credential']); unset($values['id']); // Check if email is empty @@ -158,6 +157,68 @@ public function avatarAction() $this->view()->setTemplate('edit-user'); } + /** + * Display user avatar and delete + */ + public function passwordAction() + { + $uid = _get('uid'); + + // Get user basic information and user data + $user = $this->getUser($uid); + + // Set option + $option = []; + + // Set form + $form = new EditPasswordForm('password', $option); + if ($this->request->isPost()) { + + // Set result + $result = [ + 'status' => 0, + 'message' => _a('User password update failed.'), + ]; + + $form->setData($this->request->getPost()); + $form->setInputFilter(new EditPasswordFilter($option)); + if ($form->isValid()) { + + // Update user + $values = $form->getData(); + + // Set update values + $updateValues = [ + 'credential' => $values['credential-new'], + 'last_modified' => time(), + ]; + + // Delete user avatar + $status = Pi::api('user', 'user')->updateUser($uid, $updateValues); + + // Check status + if ($status) { + $result = [ + 'status' => 1, + 'message' => _a('User password change successful.'), + ]; + Pi::service('event')->trigger('user_update', $uid); + } + $this->view()->assign('result', $result); + } + } + + $this->view()->assign( + [ + 'user' => $user, + 'nav' => $this->getNav($uid), + 'name' => 'password', + 'form' => $form, + ] + ); + $this->view()->setTemplate('edit-user'); + } + /** * Edit user compound * @@ -308,7 +369,10 @@ protected function getEditField() $elements = []; $filters = []; - $meta = Pi::registry('field', 'user')->read(); + // Get meta + $meta = Pi::registry('field', 'user')->read(); + unset($meta['credential']); + $editFields = []; foreach ($meta as $row) { if ($row['edit'] && $row['type'] != 'compound') { @@ -357,6 +421,20 @@ protected function getNav($uid) 'link' => $this->url('', ['controller' => 'edit', 'uid' => $uid]), ]; + // Password + $result[] = [ + 'name' => 'password', + 'title' => _a('Password'), + 'link' => $this->url( + '', + [ + 'controller' => 'edit', + 'action' => 'password', + 'uid' => $uid, + ] + ), + ]; + // Avatar $result[] = [ 'name' => 'avatar', @@ -364,10 +442,10 @@ protected function getNav($uid) 'link' => $this->url( '', [ - 'controller' => 'edit', - 'action' => 'avatar', - 'uid' => $uid, - ] + 'controller' => 'edit', + 'action' => 'avatar', + 'uid' => $uid, + ] ), ]; @@ -387,11 +465,11 @@ protected function getNav($uid) 'link' => $this->url( '', [ - 'controller' => 'edit', - 'action' => 'compound', - 'uid' => $uid, - 'name' => $row['name'], - ] + 'controller' => 'edit', + 'action' => 'compound', + 'uid' => $uid, + 'name' => $row['name'], + ] ), ]; } @@ -403,10 +481,10 @@ protected function getNav($uid) 'link' => $this->url( '', [ - 'controller' => 'View', - 'action' => 'index', - 'uid' => $uid, - ] + 'controller' => 'view', + 'action' => 'index', + 'uid' => $uid, + ] ), ]; diff --git a/usr/module/user/src/Controller/Admin/ViewController.php b/usr/module/user/src/Controller/Admin/ViewController.php index 443c73dcac..2fd8844936 100644 --- a/usr/module/user/src/Controller/Admin/ViewController.php +++ b/usr/module/user/src/Controller/Admin/ViewController.php @@ -96,6 +96,20 @@ protected function getNav($uid) 'link' => $this->url('', ['controller' => 'edit', 'uid' => $uid]), ]; + // Password + $result[] = [ + 'name' => 'password', + 'title' => _a('Password'), + 'link' => $this->url( + '', + [ + 'controller' => 'edit', + 'action' => 'password', + 'uid' => $uid, + ] + ), + ]; + // Avatar $result[] = [ 'name' => 'avatar', @@ -134,7 +148,7 @@ protected function getNav($uid) 'name' => 'view', 'title' => _a('View'), 'link' => $this->url('', [ - 'controller' => 'View', + 'controller' => 'view', 'action' => 'index', 'uid' => $uid, ]), diff --git a/usr/module/user/src/Form/EditPasswordFilter.php b/usr/module/user/src/Form/EditPasswordFilter.php new file mode 100644 index 0000000000..d2e22ffffd --- /dev/null +++ b/usr/module/user/src/Form/EditPasswordFilter.php @@ -0,0 +1,61 @@ + + */ + +namespace Module\User\Form; + +use Zend\InputFilter\InputFilter; +use Module\System\Validator\UserEmail as UserEmailValidator; + +class EditPasswordFilter extends InputFilter +{ + public function __construct($option = []) + { + $this->add( + [ + 'name' => 'credential-new', + 'required' => true, + 'filters' => [ + [ + 'name' => 'StringTrim', + ], + ], + 'validators' => [ + [ + 'name' => 'Module\User\Validator\Password', + ], + ], + ] + ); + + $this->add( + [ + 'name' => 'credential-confirm', + 'required' => true, + 'filters' => [ + [ + 'name' => 'StringTrim', + ], + ], + 'validators' => [ + [ + 'name' => 'Identical', + 'options' => [ + 'token' => 'credential-new', + 'strict' => true, + ], + ], + ], + ] + ); + } +} \ No newline at end of file diff --git a/usr/module/user/src/Form/EditPasswordForm.php b/usr/module/user/src/Form/EditPasswordForm.php new file mode 100644 index 0000000000..82e3f3a8fb --- /dev/null +++ b/usr/module/user/src/Form/EditPasswordForm.php @@ -0,0 +1,77 @@ + + */ + +namespace Module\User\Form; + +use Pi\Form\Form as BaseForm; + +class EditPasswordForm extends BaseForm +{ + public function __construct($name = null, $option = []) + { + $this->option = $option; + parent::__construct($name); + } + + public function getInputFilter() + { + if (!$this->filter) { + $this->filter = new EditPasswordFilter($this->option); + } + return $this->filter; + } + + public function init() + { + $this->add( + [ + 'name' => 'credential-new', + 'options' => [ + 'label' => __('New password'), + ], + 'attributes' => [ + 'type' => 'password', + ], + ] + ); + + $this->add( + [ + 'name' => 'credential-confirm', + 'options' => [ + 'label' => __('Confirm password'), + ], + 'attributes' => [ + 'type' => 'password', + ], + ] + ); + + $this->add( + [ + 'name' => 'security', + 'type' => 'csrf', + ] + ); + + $this->add( + [ + 'name' => 'submit', + 'type' => 'submit', + 'attributes' => [ + 'value' => __('Submit'), + ], + ] + ); + } +} \ No newline at end of file diff --git a/usr/module/user/template/admin/edit-user.phtml b/usr/module/user/template/admin/edit-user.phtml index 204c0283da..fbde527109 100644 --- a/usr/module/user/template/admin/edit-user.phtml +++ b/usr/module/user/template/admin/edit-user.phtml @@ -14,17 +14,17 @@ %s', - $result['status'] ? 'alert-success' : 'alert-error', + '
%s
', + $result['status'] ? 'alert-success' : 'alert-danger', $result['message']); } ?> - form($form); return; - } ?> - + +

%s

@@ -35,7 +35,7 @@ return; } ?> %s', _a('This user doesn\'t have any you can view')); + echo sprintf('

%s
', _a('This user doesn\'t have any you can view')); return; } ?> $form) { ?>