+++ /dev/null
-<?php
-namespace App\Model\Table;
-
-use App\Model\Entity\User;
-use Cake\ORM\Query;
-use Cake\ORM\RulesChecker;
-use Cake\ORM\Table;
-use Cake\Validation\Validator;
-
-/**
- * For authentification
- */
-use Cake\Auth\DefaultPasswordHasher;
-use Cake\Utility\Text;
-use Cake\Event\Event;
-
-/**
- * Users Model
- *
- * @property \Cake\ORM\Association\BelongsTo $UserTypes
- * @property \Cake\ORM\Association\HasMany $Reviews
- * @property \Cake\ORM\Association\BelongsToMany $RelationshipsSoftwares
- * @property \Cake\ORM\Association\BelongsToMany $Relationships
- */
-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->table('users');
- $this->displayField('username');
- $this->primaryKey('id');
-
- $this->addBehavior('Timestamp');
-
- //For upload file -> avatar
- $this->addBehavior('Josegonzalez/Upload.Upload', [
- 'photo' => [
- 'fields' => [
- 'dir' => 'logo_directory',
- ],
- 'path' => 'webroot{DS}files{DS}{model}{DS}{field}{DS}{primaryKey}{DS}avatar',
- ],
- ]);
-
- $this->belongsTo('UserTypes', [
- 'foreignKey' => 'user_type_id',
- 'joinType' => 'INNER'
- ]);
- $this->hasMany('Reviews', [
- 'foreignKey' => 'user_id'
- ]);
- $this->belongsToMany('RelationshipsSoftwares', [
- 'foreignKey' => 'user_id',
- 'targetForeignKey' => 'relationship_id',
- 'joinTable' => 'relationships_softwares_users'
- ]);
- $this->belongsToMany('Relationships', [
- 'foreignKey' => 'user_id',
- 'targetForeignKey' => 'recipient_id',
- 'joinTable' => 'relationships_users'
- ]);
- }
-
- /**
- * Default validation rules.
- *
- * @param \Cake\Validation\Validator $validator Validator instance.
- * @return \Cake\Validation\Validator
- */
- public function validationDefault(Validator $validator)
- {
- $validator
- ->allowEmpty('id', 'create');
-
- $validator
- ->allowEmpty('username');
-
- $validator
- ->allowEmpty('logo_directory');
-
- $validator
- ->allowEmpty('url');
-
- $validator
- ->allowEmpty('description');
-
- $validator
- ->requirePresence('role', 'create')
- ->notEmpty('role');
-
- $validator
- ->requirePresence('password', 'create')
- ->notEmpty('password');
-
- $validator
- ->add('email', 'valid', ['rule' => 'email'])
- ->allowEmpty('email');
-
- $validator
- ->allowEmpty('photo');
-
- 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(['username']));
- $rules->add($rules->isUnique(['email']));
- $rules->add($rules->existsIn(['user_type_id'], 'UserTypes'));
- return $rules;
- }
-
-
- /**
- * Before save an user
- * @param \App\Controller\Api\V1\Event $event
- * @return boolean
- */
- public function beforeSave(Event $event)
- {
- $entity = $event->data['entity'];
-
- if ($entity->isNew()) {
- $hasher = new DefaultPasswordHasher();
-
- // Generate an API 'token'
- $entity->api_key_plain = sha1(Text::uuid());
-
- // Bcrypt the token so BasicAuthenticate can check
- // it during login.
- $entity->api_key = $hasher->hash($entity->api_key_plain);
- }
- return true;
- }
-}