CakePHP 3 użytkownicy ciąg dalszy
W tej części utworzymy strony edycji i podglądu użytkownika. Widok edycji będzie prawie identyczny z widokiem dodawania, z tą różnicą, że dodamy do niego trochę ograniczeń, ponieważ nie chcemy, aby każdy użytkownik mógł zmienić swoją rolę z "User" na "Admin".
public function isAuthorized($user)
{
// Admin has full access
if ($user['role'] == 'Admin') {
return true;
}
// User can view and edit own account only
if (in_array($this->request->action, ['view', 'edit']) && $user['id'] == (int)$this->request->params['pass'][0]) {
return true;
}
return false;
}
Powyższy kod nadpisuje działanie funkcji isAuthorized w klasie nadrzędnej (AppController.php).
Teraz dodamy funkcję wylogowania. W tym celu w kontrolerze dodaj następującą metodę.
public function logout()
{
$this->Flash->success('You are now logged out.');
return $this->redirect($this->Auth->logout());
}
Następnie dodajmy jeszcze dwie akcje edit i view w kontrolerze.
public function view($id = null) {
$user = $this->Users->get($id);
$this->set('user', $user);
$this->set('_serialize', ['user']);
}
public function edit($id = null) {
$user = $this->Users->get($id);
if ($this->request->is(['patch', 'post', 'put'])) {
if ($this->request->data['password'] == '') {
unset($this->request->data['password']);
}
$user = $this->Users->patchEntity($user, $this->request->data);
if ($this->Users->save($user)) {
$this->Flash->success(__('The user has been saved.'));
return $this->redirect(['action' => 'index']);
} else {
$this->Flash->error(__('The user could not be saved. Please, try again.'));
}
}
unset($user->password);
$roles = $this->roles;
$this->set(compact('user', 'roles'));
$this->set('_serialize', ['user']);
}
Musimy stworzyć dwa widoki src/Templates/Users/view.ctp
<?php $this->assign('title', $user->full_name); ?>
<div class="users view large-9 medium-8 columns content">
<?php echo $this->Html->link(__('Edit User'), ['action' => 'edit', $user->id], ['class' => 'right']) ?>
<h3><?php echo h($user->full_name) ?></h3>
<table class="vertical-table">
<tr>
<th><?php echo __('Username') ?></th>
<td><?php echo h($user->username) ?></td>
</tr>
<tr>
<th><?php echo __('First Name') ?></th>
<td><?php echo h($user->first_name) ?></td>
</tr>
<tr>
<th><?php echo __('Last Name') ?></th>
<td><?php echo h($user->last_name) ?></td>
</tr>
<tr>
<th><?php echo __('Email') ?></th>
<td><?php echo $this->Text->autoLinkEmails($user->email) ?></td>
</tr>
<tr>
<th><?php echo __('Role') ?></th>
<td><?php echo h($user->role) ?></td>
</tr>
<tr>
<th><?php echo __('Modified') ?></th>
<td><?php echo h($user->modified) ?></td>
</tr>
<tr>
<th><?php echo __('Created') ?></th>
<td><?php echo h($user->created) ?></td>
</tr>
</table>
</div>
oraz src/Templates/Users/edit.ctp
<?php $this->assign('title', 'Edit User'); ?>
<div class="users form large-9 medium-8 columns content">
<?php echo $this->Form->create($user) ?>
<fieldset>
<legend><?php echo __('Edit User') ?></legend>
<?php
echo $this->Form->input('username', ['autofocus' => true]);
echo $this->Form->input('first_name');
echo $this->Form->input('last_name');
echo $this->Form->input('password'); ?>
<p class="helper">Passwords must be at least 8 characters and contain at least 1 number, 1 uppercase, 1 lowercase and 1 special character</p>
<?php
echo $this->Form->input('confirm_password', ['type' => 'password']);
echo $this->Form->input('email');
if ($this->request->session()->read('Auth.User.role') == 'Admin') { echo $this->Form->input('role'); }
?>
</fieldset>
<?php echo $this->Form->button(__('Submit')); ?>
<?php echo $this->Html->link(__('Cancel'), ['action' => 'view', $user->id], ['class' => 'button']); ?>
<?php echo $this->Form->end(); ?>
<?php echo $this->Form->postLink(__('Delete User'), ['action' => 'delete', $user->id], ['confirm' => __('Are you sure you want to delete user: {0}?', $user->full_name)]); ?>
</div>
Zauważ, że w widoku edycji pole do zmiany roli wyświetlamy tylko dla adminów.
