Last active
September 6, 2019 12:11
-
-
Save MayMeow/03127b8e2ea7e6d35f9ee55cef3988a1 to your computer and use it in GitHub Desktop.
Users -> Profiles
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @var \App\View\AppView $this | |
* @var \Cake\Datasource\EntityInterface $user | |
*/ | |
$this->extend('CakeBootstrap./_Backend/Simple/fluid'); | |
?> | |
<!-- Hero --> | |
<div class="row"> | |
<div class="col-md-12"> | |
<h1><?= __('New Client') ?></h1> | |
</div> | |
</div> | |
<!-- END Hero --> | |
<!-- Page Content --> | |
<div class="row"> | |
<div class="col-md-12"> | |
<div class="text-right mb-3"> | |
<?= $this->Html->link('<i class="far fa-list-alt"></i>', ['controller' => 'addresses', 'action' => 'index'], ['class' => 'btn btn-light', | |
'escape' => false]) ?> | |
</div> | |
</div> | |
</div> | |
<div class="row"> | |
<div class="col-md-12"> | |
<?= $this->Form->create($user) ?> | |
<div class="row"> | |
<div class="col-md-6"> | |
<div class="card"> | |
<div class="card-header"><?= __('Account information') ?></div> | |
<div class="card-body"> | |
<?php | |
echo $this->Form->control('email'); | |
echo $this->Form->control('password', ['label' => 'Password']); | |
echo $this->Form->control('password2', ['type' => 'password', 'label' => __('Passowrd confirmation')]); | |
//echo $this->Form->control('role_id', ['options' => $roles, 'label' => __('User type')]); | |
?> | |
<div class="mx-2"> | |
<div class="font-weight-bold"><?= __('User type')?></div> | |
<div> | |
<?= __('You are creating user with type "User" if you want create new user with different type use following link') ?> | |
<?= $this->Html->link(__('New user'), ['plugin' => 'Identity', 'controller' => 'Users', 'action' => 'add']) ?> | |
</div> | |
</div> | |
</div> | |
<div class="card-footer"> | |
<?= $this->Form->button(__('Save'), ['class' => 'btn btn-success']) ?> | |
<?= $this->Html->link(__('Cancel'), ['action' => 'index'], ['class' => 'btn btn-danger']) ?> | |
</div> | |
</div> | |
</div> | |
<div class="col-md-6"> | |
<div class="card"> | |
<div class="card-header"><?= __('Address information') ?></div> | |
<div class="card-body"> | |
<?php | |
//echo $this->Form->control('profile.user_id', ['type' => 'hidden']); | |
echo $this->Form->control('profile.name', ['label' => __('First name')]); | |
echo $this->Form->control('profile.last_name', ['label' => __('Last name')]); | |
echo $this->Form->control('addresses.0.name', ['label' => __('Company name')]); | |
echo $this->Form->control('addresses.0.street', ['label' => __('Street and house number')]); | |
echo $this->Form->control('addresses.0.postcode', ['label' => __('ZIP')]); | |
echo $this->Form->control('addresses.0.town', ['label' => __('Town'), 'value' => '']); | |
echo $this->Form->control('addresses.0.phone', ['label' => __('Phone number')]); | |
echo $this->Form->control('addresses.0.authorized_persons', ['label' => __('Authorized persons for posts take')]); | |
?> | |
</div> | |
</div> | |
</div> | |
</div> | |
<?= $this->Form->end() ?> | |
</div> | |
</div> | |
<!-- END Page Content --> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace PostBox\Model\Entity; | |
use Identity\Model\Entity\User; | |
/** | |
* Class ApplicationUser | |
* @package PostBox\Model\Entity | |
* | |
* @property \PostBox\Model\Entity\Address $addresses | |
*/ | |
class ApplicationUser extends User | |
{ | |
/** | |
* Fields that can be mass assigned using newEntity() or patchEntity(). | |
* | |
* Note that when '*' is set to true, this allows all unspecified fields to | |
* be mass assigned. For security purposes, it is advised to set '*' to false | |
* (or remove it), and explicitly make individual fields accessible as needed. | |
* | |
* @var array | |
*/ | |
protected $_accessible = [ | |
'email' => true, | |
'password' => true, | |
'created' => true, | |
'modified' => true, | |
'role_id' => true, //added this to allow create users with roles | |
'addresses' => true, // TODO can be removed outside project | |
'profile' => true | |
]; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace PostBox\Model\Table; | |
use Cake\Validation\Validator; | |
use Identity\Model\Entity\User; | |
use Identity\Model\Table\UsersTable; | |
use PostBox\Model\Entity\ApplicationUser; | |
/** | |
* Class UsersTable | |
* @package PostBox\Model\Table | |
* | |
* @property \PostBox\Model\Table\AddressesTable|\Cake\ORM\Association\HasMany $Addresses | |
* | |
* @method \PostBox\Model\Entity\ApplicationUser get($primaryKey, $options = []) | |
* @method \PostBox\Model\Entity\ApplicationUser newEntity($data = null, array $options = []) | |
* @method \PostBox\Model\Entity\ApplicationUser[] newEntities(array $data, array $options = []) | |
* @method \PostBox\Model\Entity\ApplicationUser|bool save(\Cake\Datasource\EntityInterface $entity, $options = []) | |
* @method \PostBox\Model\Entity\ApplicationUser saveOrFail(\Cake\Datasource\EntityInterface $entity, $options = []) | |
* @method \PostBox\Model\Entity\ApplicationUser patchEntity(\Cake\Datasource\EntityInterface $entity, array $data, array $options = []) | |
* @method \PostBox\Model\Entity\ApplicationUser[] patchEntities($entities, array $data, array $options = []) | |
* @method \PostBox\Model\Entity\ApplicationUser findOrCreate($search, callable $callback = null, $options = []) | |
* | |
*/ | |
class ApplicationUsersTable extends UsersTable | |
{ | |
public function initialize(array $config) | |
{ | |
parent::initialize($config); // TODO: Change the autogenerated stub | |
$this->setEntityClass(ApplicationUser::class); | |
$this->hasMany('Addresses', [ | |
'foreignKey' => 'user_id', | |
'className' => 'PostBox.Addresses' | |
]); | |
} | |
public function validationDefault(Validator $validator) | |
{ | |
parent::validationDefault($validator); // TODO: Change the autogenerated stub | |
return $validator; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* ... */ | |
/** | |
* @throws \Exception | |
*/ | |
public function initialize() | |
{ | |
parent::initialize(); // TODO: Change the autogenerated stub | |
$this->loadComponent('Identity.User'); | |
$this->loadModel('PostBox.ApplicationUsers'); | |
$this->set('cake_php_backend_menu', Configure::read('Menu.backend')); | |
} | |
public function add() | |
{ | |
$user = $this->ApplicationUsers->newEntity(); | |
if ($this->request->is('post') && $this->_passwordConfirm($this->request)) { | |
$user = $this->ApplicationUsers->patchEntity($user, $this->request->getData()); | |
$newRole = $this->ApplicationUsers->Roles->find()->select(['Roles.id'])->where(['Roles.name' => 'Users'])->first(); | |
$user->role_id = $newRole->id; | |
if ($this->ApplicationUsers->save($user)) { | |
$this->Flash->success(__('The user has been saved.')); | |
return $this->redirect(['controller' => 'addresses', 'action' => 'index']); | |
} | |
$this->Flash->error(__('The user could not be saved. Please, try again.')); | |
} else { | |
$this->Flash->error(__('Please enter correct password and password confirmation')); | |
} | |
$roles = $this->ApplicationUsers->Roles->find('list', ['limit' => 200])->order(['Roles.weight' => 'DESC']); | |
$this->set(compact('user', 'roles')); | |
} | |
protected function _passwordConfirm(ServerRequest $request) | |
{ | |
if ($request->getData('password') == null || $request->getData('password2') == null) { | |
return false; | |
} | |
if ($request->getData('password') !== $request->getData('password2')) return false; | |
return true; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
object(PostBox\Model\Entity\ApplicationUser) { | |
'email' => '[email protected]', | |
'password' => '$2y$10$Eb4jsw07RsV59AgRb2f3V.kAhR35tQCtvm74y/ePqyW2RhFD2./r6', | |
'profile' => object(Identity\Model\Entity\Profile) { | |
'name' => 'Janka', | |
'last_name' => 'xxxxx', | |
'[new]' => true, | |
'[accessible]' => [ | |
'user_id' => true, | |
'name' => true, | |
'last_name' => true, | |
'user' => true | |
], | |
'[dirty]' => [ | |
'name' => true, | |
'last_name' => true | |
], | |
'[original]' => [], | |
'[virtual]' => [], | |
'[hasErrors]' => false, | |
'[errors]' => [], | |
'[invalid]' => [], | |
'[repository]' => 'Identity.Profiles' | |
}, | |
'addresses' => [ | |
(int) 0 => object(PostBox\Model\Entity\Address) { | |
'name' => 'free2', | |
'street' => 'Street 99', | |
'postcode' => '09090', | |
'town' => 'test', | |
'phone' => '+42194804xxxxx', | |
'authorized_persons' => '', | |
'[new]' => true, | |
'[accessible]' => [ | |
'user_id' => true, | |
'name' => true, | |
'street' => true, | |
'postcode' => true, | |
'user' => true, | |
'phone' => true, | |
'town' => true, | |
'authorized_persons' => true, | |
'posts' => true | |
], | |
'[dirty]' => [ | |
'name' => true, | |
'street' => true, | |
'postcode' => true, | |
'town' => true, | |
'phone' => true, | |
'authorized_persons' => true | |
], | |
'[original]' => [], | |
'[virtual]' => [], | |
'[hasErrors]' => false, | |
'[errors]' => [], | |
'[invalid]' => [], | |
'[repository]' => 'PostBox.Addresses' | |
} | |
], | |
'[new]' => true, | |
'[accessible]' => [ | |
'email' => true, | |
'password' => true, | |
'created' => true, | |
'modified' => true, | |
'role_id' => true, | |
'addresses' => true, | |
'profile' => true | |
], | |
'[dirty]' => [ | |
'email' => true, | |
'password' => true, | |
'profile' => true, | |
'addresses' => true | |
], | |
'[original]' => [], | |
'[virtual]' => [], | |
'[hasErrors]' => true, | |
'[errors]' => [ | |
'role_id' => [ | |
'_required' => 'This field is required' | |
] | |
], | |
'[invalid]' => [], | |
'[repository]' => 'PostBox.ApplicationUsers' | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Identity\Model\Entity; | |
use Cake\Auth\DefaultPasswordHasher; | |
use Cake\ORM\Entity; | |
/** | |
* User Entity | |
* | |
* @property int $id | |
* @property string $email | |
* @property string $password | |
* @property int $role_id | |
* | |
* @property \Cake\I18n\FrozenTime $created | |
* @property \Cake\I18n\FrozenTime $modified | |
*/ | |
class User extends Entity | |
{ | |
/** | |
* Fields that can be mass assigned using newEntity() or patchEntity(). | |
* | |
* Note that when '*' is set to true, this allows all unspecified fields to | |
* be mass assigned. For security purposes, it is advised to set '*' to false | |
* (or remove it), and explicitly make individual fields accessible as needed. | |
* | |
* @var array | |
*/ | |
protected $_accessible = [ | |
'email' => true, | |
'password' => true, | |
'created' => true, | |
'modified' => true, | |
'role_id' => true, //added this to allow create users with roles | |
'profile' => true | |
]; | |
/** | |
* Fields that are excluded from JSON versions of the entity. | |
* | |
* @var array | |
*/ | |
protected $_hidden = [ | |
'password' | |
]; | |
// Add this method | |
protected function _setPassword($value) | |
{ | |
if (strlen($value)) { | |
$hasher = new DefaultPasswordHasher(); | |
return $hasher->hash($value); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Identity\Model\Table; | |
use Cake\Auth\DefaultPasswordHasher; | |
use Cake\Event\Event; | |
use Cake\ORM\Query; | |
use Cake\ORM\RulesChecker; | |
use Cake\ORM\Table; | |
use Cake\Utility\Security; | |
use Cake\Validation\Validator; | |
/** | |
* Users Model | |
* | |
* @property \Identity\Model\Table\RolesTable|\Cake\ORM\Association\BelongsToMany $Groups | |
* @property \Identity\Model\Table\RolesTable|\Cake\ORM\Association\BelongsTo $Roles | |
* | |
* @method \Identity\Model\Entity\User get($primaryKey, $options = []) | |
* @method \Identity\Model\Entity\User newEntity($data = null, array $options = []) | |
* @method \Identity\Model\Entity\User[] newEntities(array $data, array $options = []) | |
* @method \Identity\Model\Entity\User|bool save(\Cake\Datasource\EntityInterface $entity, $options = []) | |
* @method \Identity\Model\Entity\User saveOrFail(\Cake\Datasource\EntityInterface $entity, $options = []) | |
* @method \Identity\Model\Entity\User patchEntity(\Cake\Datasource\EntityInterface $entity, array $data, array $options = []) | |
* @method \Identity\Model\Entity\User[] patchEntities($entities, array $data, array $options = []) | |
* @method \Identity\Model\Entity\User findOrCreate($search, callable $callback = null, $options = []) | |
* | |
* @mixin \Cake\ORM\Behavior\TimestampBehavior | |
*/ | |
class UsersTable extends Table | |
{ | |
/** | |
* Initialize method | |
* | |
* @param array $config The configuration for the Table. | |
* @return void | |
*/ | |
public function initialize(array $config) | |
{ | |
parent::initialize($config); | |
$this->setTable('cake_php_users'); | |
$this->setDisplayField('email'); | |
$this->setPrimaryKey('id'); | |
$this->addBehavior('Timestamp'); | |
$this->belongsTo('Roles', [ | |
'className' => 'Identity.Roles', | |
'foreignKey' => 'role_id', | |
'joinType' => 'INNER' | |
]); | |
$this->hasOne('Profiles', [ | |
'className' => 'Identity.Profiles' | |
]); | |
$this->hasMany('Secrets', [ | |
'className' => 'Identity.Secrets' | |
]); | |
} | |
/** | |
* Default validation rules. | |
* | |
* @param \Cake\Validation\Validator $validator Validator instance. | |
* @return \Cake\Validation\Validator | |
*/ | |
public function validationDefault(Validator $validator) | |
{ | |
$validator | |
->integer('id') | |
->allowEmptyString('id', null, 'create'); | |
$validator | |
->email('email') | |
->requirePresence('email', 'create') | |
->allowEmptyString('email', null, false) | |
->add('email', 'unique', ['rule' => 'validateUnique', 'provider' => 'table']); | |
$validator | |
->scalar('password') | |
->maxLength('password', 255) | |
->requirePresence('password', 'create') | |
->allowEmptyString('password', null, false); | |
$validator | |
->integer('role_id') | |
->requirePresence('role_id', 'create'); | |
return $validator; | |
} | |
/** | |
* Returns a rules checker object that will be used for validating | |
* application integrity. | |
* | |
* @param \Cake\ORM\RulesChecker $rules The rules object to be modified. | |
* @return \Cake\ORM\RulesChecker | |
*/ | |
public function buildRules(RulesChecker $rules) | |
{ | |
$rules->add($rules->isUnique(['email'])); | |
$rules->add($rules->existsIn(['role_id'], 'Roles')); | |
return $rules; | |
} | |
public function beforeSave(Event $event) | |
{ | |
$entity = $event->getData('entity'); | |
if ($entity->isNew()) { | |
$hasher = new DefaultPasswordHasher(); | |
$entity->api_key_plain = Security::hash(Security::randomBytes(32), 'sha256', false); | |
$entity->api_key = $hasher->hash($entity->api_key_plain); | |
} | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment