Created
October 20, 2013 23:05
-
-
Save andrebian/7076442 to your computer and use it in GitHub Desktop.
Modelo de usuários
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 | |
App::uses('AppModel', 'Model'); | |
/** | |
* User Model | |
* | |
* @property Group $Group | |
*/ | |
class User extends AppModel { | |
public $errors = array(); | |
/** | |
* Validation rules | |
* | |
* @var array | |
* | |
*/ | |
public $validate = array( | |
'group_id' => array( | |
'numeric' => array( | |
'rule' => array('numeric'), | |
'message' => 'Por favor informe o perfil do novo usuário', | |
'on' => 'create', // Limit validation to 'create' or 'update' operations | |
), | |
), | |
'name' => array( | |
'notempty' => array( | |
'rule' => array('notempty'), | |
'message' => 'Por favor informe o nome', | |
), | |
), | |
'surname' => array( | |
'notempty' => array( | |
'rule' => array('notempty'), | |
'message' => 'Por favor informe o sobrenome', | |
), | |
), | |
'email' => array( | |
'email' => array( | |
'rule' => array('email', true), | |
'message' => 'Por favor informe um email válido', | |
), | |
'emailOnInactiveState' => array( | |
'rule' => array('emailOnInactiveState'), | |
'message' => 'Este email já está cadastrado em nosso sistema.', | |
'on' => 'create', | |
), | |
'isUnique' => array( | |
'rule' => array('isUnique'), | |
'message' => 'Este email já está cadastrado em nosso sistema.', | |
'on' => 'update' | |
) | |
), | |
'username' => array( | |
'notempty' => array( | |
'rule' => array('notempty'), | |
'message' => 'Por favor informe o login', | |
), | |
), | |
'password' => array( | |
'notempty' => array( | |
'rule' => array('notempty'), | |
'message' => 'Por favor informe a senha', | |
'on' => 'create', // Limit validation to 'create' or 'update' operations | |
), | |
), | |
'password_confirm' => array( | |
'comparePasswords' => array( | |
'rule' => array('comparePasswords'), | |
'message' => 'As senhas digitadas não conferem, por favor verifique', | |
), | |
'notempty' => array( | |
'rule' => array('notempty'), | |
'message' => 'Por favor confirme a senha', | |
'on' => 'create', // Limit validation to 'create' or 'update' operations | |
), | |
), | |
'address' => array( | |
'notempty' => array( | |
'rule' => array('notempty'), | |
'message' => 'Por favor informe o endereço', | |
), | |
), | |
'addr_number' => array( | |
'numeric' => array( | |
'rule' => array('numeric'), | |
'message' => 'Informe um número válido', | |
), | |
), | |
'addr_district' => array( | |
'notempty' => array( | |
'rule' => array('notempty'), | |
'message' => 'Informe o bairro', | |
), | |
), | |
'addr_city' => array( | |
'notempty' => array( | |
'rule' => array('notempty'), | |
'message' => 'Por favor informe a cidade', | |
), | |
), | |
'addr_state' => array( | |
'notempty' => array( | |
'rule' => array('notempty'), | |
'message' => 'Por favor informe o estado', | |
), | |
), | |
'addr_country' => array( | |
'notempty' => array( | |
'rule' => array('notempty'), | |
'message' => 'Por favor informe o País', | |
), | |
), | |
'addr_zip_code' => array( | |
'notempty' => array( | |
'rule' => array('notempty'), | |
'message' => 'Informe o CEP', | |
), | |
), | |
'phone' => array( | |
'notempty' => array( | |
'rule' => array('notempty'), | |
'message' => 'Por favor informe o telefone principal', | |
), | |
), | |
'aceito' => array( | |
'notempty' => array( | |
'rule' => array('notempty'), | |
'message' => 'Você deve concordar com os termos de uso para realizar o cadastro' | |
) | |
) | |
); | |
public function emailOnInactiveState( $data = array() ) | |
{ | |
if ( !empty( $data ) ) { | |
$user = $this->find('first', array('conditions' => array('email' => $data['email']))); | |
if ( $user ) { | |
if ( $user['User']['status'] == 'active' ) { | |
return false; | |
} else { | |
$this->id = $user['User']['id']; | |
return true; | |
} | |
} | |
} | |
return false; | |
} | |
/** | |
* | |
* @param array $data | |
* @return boolean | |
*/ | |
public function comparePasswords($data = array()) { | |
if (isset($this->data[$this->alias]['password'])) { | |
return $this->data[$this->alias]['password'] === current($data); | |
} | |
} | |
/** | |
* | |
* @return string | |
*/ | |
public function passRecover() { | |
return substr(md5(date('siHdmY')),0,6); | |
} | |
/** | |
* | |
* @param array $options | |
*/ | |
public function beforeValidate($options = array()) | |
{ | |
if ( isset($this->data[$this->alias]['aceito']) ) { | |
if ( $this->data[$this->alias]['aceito'] == '0' ) { | |
$this->data[$this->alias]['aceito'] = ''; | |
} | |
} | |
if ( isset($this->data[$this->alias]['fast']) && $this->data[$this->alias]['fast'] == true || | |
isset($this->data[$this->alias]['facebook_id'])) { | |
if ( !isset($this->data[$this->alias]['facebook_id']) ) { | |
$this->data['User']['username'] = $this->data[$this->alias]['email']; | |
$this->data[$this->alias]['addr_city'] = 'Não definida'; | |
$this->data[$this->alias]['addr_country'] = 'Brasil'; | |
} else { | |
$this->validator()->remove('email'); | |
$this->validator()->remove('username'); | |
$this->validator()->remove('password'); | |
$this->validator()->remove('password_confirm'); | |
$this->validator()->remove('group_id'); | |
$this->data[$this->alias]['aceito'] = 1; | |
} | |
$this->data[$this->alias]['address'] = 'Não definido'; | |
$this->data[$this->alias]['addr_number'] = 999; | |
$this->data[$this->alias]['addr_complement'] = ''; | |
$this->data[$this->alias]['addr_district'] = 'Não definido'; | |
$this->data[$this->alias]['addr_state'] = 'ND'; | |
$this->data[$this->alias]['addr_zip_code'] = '00.000-000'; | |
$this->data[$this->alias]['phone'] = '(00)0000-0000'; | |
unset($this->data[$this->alias]['fast']); | |
} | |
parent::beforeValidate($options); | |
} | |
/** | |
* | |
*/ | |
public function afterValidate() | |
{ | |
if ( $this->validationErrors ) { | |
foreach($this->validationErrors as $erro) { | |
foreach($erro as $er) { | |
array_push($this->errors, $er); | |
} | |
} | |
} | |
parent::afterValidate(); | |
} | |
/** | |
* | |
* @param array $options | |
*/ | |
public function beforeSave($options = array()) { | |
if ( isset($this->data[$this->alias]['password']) ) { | |
$pass = &$this->data[$this->alias]['password']; | |
if ( !empty($pass) ) { | |
$pass = Security::hash($pass); | |
} else { | |
unset($this->data[$this->alias]['password']); | |
} | |
} | |
if ( isset($this->data[$this->alias]['id']) && !empty($this->data[$this->alias]['id']) ) { | |
if ( isset($this->data[$this->alias]['email']) && !empty($this->data[$this->alias]['email']) ) { | |
if ( !isset($this->data[$this->alias]['username']) || $this->data[$this->alias]['username'] == '' ) { | |
$this->data[$this->alias]['username'] = $this->data[$this->alias]['email']; | |
} | |
} | |
} | |
if ( !empty($this->data[$this->alias]['avatar']['name']) && isset($this->data[$this->alias]['id']) ) { | |
$this->data[$this->alias]['avatar'] = $this->__uploadFile($this->data[$this->alias]['avatar'], $this->data[$this->alias]['id']); | |
} else if( !isset($this->data[$this->alias]['facebook_id']) ) { | |
unset($this->data[$this->alias]['avatar']); | |
} | |
parent::beforeSave($options); | |
} | |
/** | |
* | |
* @param array $fileData | |
* @param int $userId | |
* @return string | |
*/ | |
private function __uploadFile( $fileData, $userId ) | |
{ | |
if ( !is_dir('files/users/'.$userId) ) { | |
mkdir('files/users/'.$userId); | |
} else { | |
$file = $this->read('avatar', $userId); | |
} | |
$fileName = 'avatar' . substr($fileData['name'],-4); | |
if (move_uploaded_file($fileData['tmp_name'], 'files/users/'.$userId.'/'.$fileName) ) { | |
return $fileName; | |
} | |
} | |
/** | |
* | |
* @param int $userId | |
* @param boolean $production | |
* @return boolean | |
*/ | |
public function removeImagePath( $userId, $production = true ) | |
{ | |
$status = false; | |
if ( is_dir('files/users/'.$userId) ) { | |
if ( $production ) { | |
$dir = opendir('files/users/'.$userId); | |
while($file = readdir($dir)) { | |
if ( $file != '.' && $file != '..' ) { | |
@unlink('files/users/'.$userId.'/'.$file); | |
} | |
} | |
} | |
@rmdir('files/users/'.$userId); | |
$status = true; | |
} | |
return $status; | |
} | |
//The Associations below have been created with all possible keys, those that are not needed can be removed | |
/** | |
* belongsTo associations | |
* | |
* @var array | |
*/ | |
public $belongsTo = array( | |
'Group' => array( | |
'className' => 'Group', | |
'foreignKey' => 'group_id', | |
'conditions' => '', | |
'fields' => '', | |
'order' => '' | |
) | |
); | |
public $hasMany = array('UsersSkill'); | |
public $hasAndBelongsToMany = array('Materia'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment