| 1 |
<?php |
| 2 |
include_once( NTS_SYSTEM_APPPATH . '/core/MY_Model.php' ); |
| 3 |
class User_HC_Model extends MY_model |
| 4 |
{ |
| 5 |
const LEVEL_STAFF = 1; |
| 6 |
const LEVEL_MANAGER = 2; |
| 7 |
const LEVEL_ADMIN = 3; |
| 8 |
|
| 9 |
const STATUS_ACTIVE = 1; |
| 10 |
const STATUS_ARCHIVE = 0; |
| 11 |
|
| 12 |
var $salt_length = 10; |
| 13 |
var $table = 'users'; |
| 14 |
var $default_order_by = array('active' => 'DESC', 'last_name' => 'ASC', 'first_name' => 'ASC'); |
| 15 |
|
| 16 |
var $has_many = array( |
| 17 |
'shift' => array( |
| 18 |
'class' => 'shift', |
| 19 |
'other_field' => 'user', |
| 20 |
), |
| 21 |
); |
| 22 |
|
| 23 |
var $validation = array( |
| 24 |
'first_name' => array('required', 'trim', 'max_length' => 50), |
| 25 |
'last_name' => array('trim', 'max_length' => 50), |
| 26 |
'email' => array('required', 'trim', 'valid_email', 'unique'), |
| 27 |
'username' => array('default_username', 'required', 'trim', 'unique'), |
| 28 |
'password' => array('required', 'trim', 'hash_password'), |
| 29 |
'confirm_password' => array('required', 'trim', 'hash_password', 'matches' => 'password'), |
| 30 |
'token' => array('make_token'), |
| 31 |
'level' => array( |
| 32 |
'enum' => array( |
| 33 |
self::LEVEL_STAFF, |
| 34 |
self::LEVEL_MANAGER, |
| 35 |
self::LEVEL_ADMIN |
| 36 |
) |
| 37 |
), |
| 38 |
'active' => array( |
| 39 |
'enum' => array( |
| 40 |
self::STATUS_ACTIVE, |
| 41 |
self::STATUS_ARCHIVE |
| 42 |
) |
| 43 |
), |
| 44 |
); |
| 45 |
|
| 46 |
public function get_admins( $shift = NULL ) |
| 47 |
{ |
| 48 |
$this |
| 49 |
->where_in( 'level', array($this->_const('LEVEL_MANAGER'), $this->_const('LEVEL_ADMIN')) ) |
| 50 |
->where( 'active', $this->_const('STATUS_ACTIVE') ) |
| 51 |
; |
| 52 |
$this->get(); |
| 53 |
return $this; |
| 54 |
} |
| 55 |
|
| 56 |
public function auth_token() |
| 57 |
{ |
| 58 |
if( ! strlen($this->token) ){ |
| 59 |
$this->token = HC_Lib::generate_rand(); |
| 60 |
$this->save(); |
| 61 |
} |
| 62 |
return $this->token; |
| 63 |
} |
| 64 |
|
| 65 |
public function get_staff() |
| 66 |
{ |
| 67 |
$app_conf = HC_App::app_conf(); |
| 68 |
$working_levels = $app_conf->get('staff:working_levels'); |
| 69 |
|
| 70 |
$this->clear(); |
| 71 |
/* get those users who can be assigned to shifts */ |
| 72 |
$this->where('active', self::STATUS_ACTIVE); |
| 73 |
if( $working_levels ){ |
| 74 |
if( ! is_array($working_levels) ) |
| 75 |
$working_levels = array( $working_levels ); |
| 76 |
$this->where_in('level', $working_levels); |
| 77 |
} |
| 78 |
$this->get(); |
| 79 |
return $this; |
| 80 |
} |
| 81 |
|
| 82 |
public function delete($object = '', $related_field = '') |
| 83 |
{ |
| 84 |
// if something is given, then just pass it over, the caller must be knowing what he's doing |
| 85 |
if( $object ){ |
| 86 |
return parent::delete( $object, $related_field ); |
| 87 |
} |
| 88 |
// if empty then delete all has_many and has_one |
| 89 |
else { |
| 90 |
$has = array_merge( array_keys($this->has_one), array_keys($this->has_many) ); |
| 91 |
foreach ( $has as $rfield ){ |
| 92 |
$this->{$rfield}->get()->delete_all(); |
| 93 |
} |
| 94 |
return parent::delete(); |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
/* check password */ |
| 99 |
function check_password( $pass ) |
| 100 |
{ |
| 101 |
if( isset($this->username) && strlen($this->username) ){ |
| 102 |
$this->get_by_username( $this->username ); |
| 103 |
} |
| 104 |
else { |
| 105 |
$this->get_by_email( $this->email ); |
| 106 |
} |
| 107 |
|
| 108 |
if ( ! $this->exists() ){ |
| 109 |
return FALSE; |
| 110 |
} |
| 111 |
|
| 112 |
$this->salt = substr($this->password, 0, $this->salt_length); |
| 113 |
$this->password = $pass; |
| 114 |
|
| 115 |
$this->validate(); |
| 116 |
$this->get(); |
| 117 |
|
| 118 |
if ( ! $this->exists() ){ |
| 119 |
return FALSE; |
| 120 |
} |
| 121 |
return TRUE; |
| 122 |
} |
| 123 |
|
| 124 |
/* validation methods */ |
| 125 |
function _hash_password( $field ) |
| 126 |
{ |
| 127 |
if (!empty($this->{$field})){ |
| 128 |
// Generate a random salt if empty |
| 129 |
if (empty($this->salt)){ |
| 130 |
$this->salt = substr( md5(uniqid(rand(), true)), 0, $this->salt_length ); |
| 131 |
} |
| 132 |
$this->{$field} = $this->salt . substr( sha1($this->salt . $this->{$field}), 0, -$this->salt_length ); |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
function _make_token( $field ) |
| 137 |
{ |
| 138 |
if( empty($this->{$field}) ){ |
| 139 |
$this->{$field} = HC_Lib::generate_rand(); |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
function _default_username( $field ) |
| 144 |
{ |
| 145 |
if( empty($this->{$field}) ){ |
| 146 |
$this->{$field} = $this->email; |
| 147 |
} |
| 148 |
} |
| 149 |
} |