Skip to content

Commit

Permalink
Add password edit page on admin side
Browse files Browse the repository at this point in the history
  • Loading branch information
voltan committed Jan 27, 2021
1 parent 01101cd commit 2121e01
Show file tree
Hide file tree
Showing 7 changed files with 262 additions and 32 deletions.
2 changes: 1 addition & 1 deletion usr/module/user/config/module.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
4 changes: 2 additions & 2 deletions usr/module/user/config/user.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down
118 changes: 98 additions & 20 deletions usr/module/user/src/Controller/Admin/EditController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,16 @@
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;

/**
* Edit user controller
*
* @author Liu Chuang <[email protected]>
* @author Hossein Azizabadi <[email protected]>
*/
class EditController extends ActionController
{
Expand All @@ -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[] = [
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
*
Expand Down Expand Up @@ -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') {
Expand Down Expand Up @@ -357,17 +421,31 @@ 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',
'title' => _a('Avatar'),
'link' => $this->url(
'',
[
'controller' => 'edit',
'action' => 'avatar',
'uid' => $uid,
]
'controller' => 'edit',
'action' => 'avatar',
'uid' => $uid,
]
),
];

Expand All @@ -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'],
]
),
];
}
Expand All @@ -403,10 +481,10 @@ protected function getNav($uid)
'link' => $this->url(
'',
[
'controller' => 'View',
'action' => 'index',
'uid' => $uid,
]
'controller' => 'view',
'action' => 'index',
'uid' => $uid,
]
),
];

Expand Down
16 changes: 15 additions & 1 deletion usr/module/user/src/Controller/Admin/ViewController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -134,7 +148,7 @@ protected function getNav($uid)
'name' => 'view',
'title' => _a('View'),
'link' => $this->url('', [
'controller' => 'View',
'controller' => 'view',
'action' => 'index',
'uid' => $uid,
]),
Expand Down
61 changes: 61 additions & 0 deletions usr/module/user/src/Form/EditPasswordFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php
/**
* Pi Engine (http://piengine.org)
*
* @link http://code.piengine.org for the Pi Engine source repository
* @copyright Copyright (c) Pi Engine http://piengine.org
* @license http://piengine.org/license.txt New BSD License
*/

/**
* @author Hossein Azizabadi <[email protected]>
*/

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,
],
],
],
]
);
}
}
77 changes: 77 additions & 0 deletions usr/module/user/src/Form/EditPasswordForm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php
/**
* Pi Engine (http://piengine.org)
*
* @link http://code.piengine.org for the Pi Engine source repository
* @copyright Copyright (c) Pi Engine http://piengine.org
* @license http://piengine.org/license.txt New BSD License
*/

/**
* @author Hossein Azizabadi <[email protected]>
*/

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'),
],
]
);
}
}
Loading

0 comments on commit 2121e01

Please sign in to comment.