Last active
August 29, 2015 14:04
-
-
Save PoeticIntensity/08454f931ced6dc0efbe to your computer and use it in GitHub Desktop.
This file contains hidden or 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
object(App\Model\Entity\User) { | |
'new' => true, | |
'accessible' => [ | |
'password' => '*****', | |
'username' => true, | |
'last_login' => true, | |
'last_ip' => true, | |
'type' => true, | |
'admins' => true, | |
'agents' => true, | |
'clients' => true, | |
'logins' => true | |
], | |
'properties' => [ | |
'type' => 'super' | |
], | |
'dirty' => [ | |
'type' => true | |
], | |
'virtual' => [], | |
'errors' => [ | |
'password' => '*****', | |
'username' => [ | |
(int) 0 => 'This field is required' | |
] | |
], | |
'repository' => 'Users' | |
} |
This file contains hidden or 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
public function add() { | |
$this->loadModel('Users'); | |
//debug($this->request->data); | |
$admin = $this->Admins->newEntity($this->request->data); | |
$user = $this->Users->newEntity($this->request->data); | |
if ($this->request->is('post')) { | |
$user->type = $this->request->data["is_super"] ? 'super' : 'admin'; | |
//WE NEED TO VALIDATE BOTH THE NEW USER AND THE NEW ADMIN BEFORE WE SAVE ANYTHING. (BY DEFAULT, IT WILL ONLY VALIDATE THE USER BEFORE SAVING THE USER) | |
if($this->Users->validate($user)) { | |
if($this->Admins->validate($admin)) { | |
if($this->Users->save($user)) { | |
$this->Flash->success('The credentials have been saved.'); | |
//UPDATE THE user_id CORRECTLY FOR THE NEW ADMIN. | |
$admin->user_id = $user->id; | |
if ($this->Admins->save($admin)) { | |
$this->Flash->success('The admin has been saved.'); | |
return $this->redirect(['action' => 'index']); | |
exit; | |
} else { | |
//NONE OF THIS FAIL CODE SHOULD EVER BE RUN, DUE TO THE PREVIOUS VALIDATION, BUT JUST IN CASE, I'M LEAVING IT HERE. | |
//GOTTA GET RID OF THE USER THAT WAS JUST SAVED NOW. | |
$this->Users->delete($user); | |
$this->Flash->error('The admin was not saved.'); | |
} | |
} else { | |
$this->Flash->error('The credentials were not saved.'); | |
} | |
} else { | |
$this->Flash->error('Please check your information and try again.'); | |
} | |
} else { | |
$errors = $user->errors(); | |
$this->Flash->error($errors["username"]["unique"]); | |
} | |
//debug($user); | |
//exit; | |
} | |
$users = $this->Admins->Users->find('list'); | |
$this->set(compact('admin', 'users')); | |
} |
This file contains hidden or 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 App\Model\Table; | |
use Cake\ORM\Table; | |
use Cake\Validation\Validator; | |
/** | |
* Users Model | |
*/ | |
class UsersTable extends Table { | |
/** | |
* Initialize method | |
* | |
* @param array $config The configuration for the Table. | |
* @return void | |
*/ | |
public function initialize(array $config) { | |
$this->table('users'); | |
$this->displayField('username'); | |
$this->primaryKey('id'); | |
$this->hasMany('Admins', [ | |
'foreignKey' => 'user_id', | |
]); | |
$this->hasMany('Agents', [ | |
'foreignKey' => 'user_id', | |
]); | |
$this->hasMany('Clients', [ | |
'foreignKey' => 'user_id', | |
]); | |
$this->hasMany('Logins', [ | |
'foreignKey' => 'user_id', | |
]); | |
} | |
/** | |
* Default validation rules. | |
* | |
* @param \Cake\Validation\Validator $validator | |
* @return \Cake\Validation\Validator | |
*/ | |
public function validationDefault(Validator $validator) { | |
$validator | |
->add('id', 'valid', ['rule' => 'numeric']) | |
->allowEmpty('id', 'create') | |
->validatePresence('username', 'create') | |
->notEmpty('username') | |
->add('username', 'unique', ['rule' => 'validateUnique', 'provider' => 'table','message' => 'It appears that username has already been taken. Please choose another one.']) | |
->validatePresence('password', 'create') | |
->notEmpty('password') | |
// ->validatePresence('last_login', 'create') | |
// ->notEmpty('last_login') | |
// ->validatePresence('last_ip', 'create') | |
// ->notEmpty('last_ip') | |
->validatePresence('type', 'create') | |
->notEmpty('type') | |
->add('type','inList', ['rule' => ['inList', ['super','admin','client','agent']]]); | |
return $validator; | |
} | |
} |
This file contains hidden or 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 App\Model\Table; | |
use Cake\ORM\Table; | |
use Cake\Validation\Validator; | |
/** | |
* Admins Model | |
*/ | |
class AdminsTable extends Table { | |
/** | |
* Initialize method | |
* | |
* @param array $config The configuration for the Table. | |
* @return void | |
*/ | |
public function initialize(array $config) { | |
$this->table('admins'); | |
$this->displayField('first_name'); | |
$this->primaryKey('id'); | |
$this->belongsTo('Users', [ | |
'foreignKey' => 'user_id', | |
]); | |
$this->hasMany('Clients', [ | |
'foreignKey' => 'admin_id', | |
]); | |
$this->hasMany('Tickets', [ | |
'foreignKey' => 'admin_id', | |
]); | |
} | |
/** | |
* Default validation rules. | |
* | |
* @param \Cake\Validation\Validator $validator | |
* @return \Cake\Validation\Validator | |
*/ | |
public function validationDefault(Validator $validator) { | |
$validator | |
->add('id', 'valid', ['rule' => 'numeric']) | |
->allowEmpty('id', 'create') | |
->validatePresence('first_name', 'create') | |
->notEmpty('first_name') | |
->validatePresence('last_name', 'create') | |
->notEmpty('last_name') | |
->add('email', 'valid', ['rule' => 'email']) | |
->validatePresence('email', 'create') | |
->notEmpty('email') | |
->add('email', 'unique', ['rule' => 'validateUnique', 'provider' => 'table','message'=>'This email is already in our system']) | |
->validatePresence('phone', 'create') | |
->notEmpty('phone') | |
->validatePresence('username', 'create') | |
->notEmpty('username') | |
->add('username', 'unique', ['rule' => 'validateUnique', 'provider' => 'table','message'=>'This username is already taken']); | |
// ->validatePresence('date_created', 'create') | |
// ->notEmpty('date_created') | |
// ->validatePresence('last_login', 'create') | |
// ->notEmpty('last_login') | |
// ->validatePresence('last_ip', 'create') | |
// ->notEmpty('last_ip') | |
// ->add('user_id', 'valid', ['rule' => 'numeric']) | |
// ->validatePresence('user_id', 'create') | |
// ->notEmpty('user_id'); | |
return $validator; | |
} | |
} |
This file contains hidden or 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
<div class="admins form"> | |
<?= $this->Form->create($admin); ?> | |
<fieldset> | |
<legend><?= __('Add Admin'); ?></legend> | |
<?php | |
echo $this->Form->input('first_name'); | |
echo $this->Form->input('last_name'); | |
echo $this->Form->input('email'); | |
echo $this->Form->input('phone'); | |
echo $this->Form->input('is_super'); | |
echo $this->Form->input('username'); | |
echo $this->Form->input('password'); | |
// echo $this->Form->input('date_created'); | |
// echo $this->Form->input('last_login'); | |
// echo $this->Form->input('last_ip'); | |
// echo $this->Form->input('user_id', ['options' => $users]); | |
?> | |
</fieldset> | |
<?= $this->Form->button(__('Submit')); ?> | |
<?= $this->Form->end(); ?> | |
</div> | |
<div class="actions"> | |
<h3><?= __('Actions'); ?></h3> | |
<ul> | |
<li><?= $this->Html->link(__('List Admins'), ['action' => 'index']); ?></li> | |
<li><?= $this->Html->link(__('List Users'), ['controller' => 'Users', 'action' => 'index']); ?> </li> | |
<li><?= $this->Html->link(__('New User'), ['controller' => 'Users', 'action' => 'add']); ?> </li> | |
<li><?= $this->Html->link(__('List Clients'), ['controller' => 'Clients', 'action' => 'index']); ?> </li> | |
<li><?= $this->Html->link(__('New Client'), ['controller' => 'Clients', 'action' => 'add']); ?> </li> | |
<li><?= $this->Html->link(__('List Tickets'), ['controller' => 'Tickets', 'action' => 'index']); ?> </li> | |
<li><?= $this->Html->link(__('New Ticket'), ['controller' => 'Tickets', 'action' => 'add']); ?> </li> | |
</ul> | |
</div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment