00001 <?php
00002 Load::models(array('userroles', 'users2userroles'));
00003
00004
00005
00006
00007
00008
00009
00010 class DAOUsers extends DataObjectTimestampedCached implements IStatusHolder, ISelfDescribing {
00011 public $id;
00012 public $name;
00013 public $password;
00014 public $hash_type;
00015 public $email;
00016 public $emailconfirmationdate;
00017 public $emailstatus;
00018 public $tos_version;
00019 public $status;
00020
00021
00022
00023 protected function create_table_object() {
00024 return new DBTable(
00025 'users',
00026 array_merge(array(
00027 new DBFieldInt('id', null, DBFieldInt::AUTOINCREMENT | DBFieldInt::UNSIGNED | DBField::NOT_NULL),
00028 new DBFieldText('name', 100, null, DBField::NOT_NULL),
00029 new DBFieldTextEmail('email', null, DBField::NOT_NULL),
00030 new DBFieldText('password', 100, null, DBField::NOT_NULL),
00031 new DBFieldText('hash_type', 5, 'md5', DBField::NOT_NULL | DBField::INTERNAL),
00032 new DBFieldDateTime('emailconfirmationdate', null, DBField::NONE | DBField::INTERNAL),
00033 new DBFieldEnum('emailstatus', array_keys(Users::get_email_statuses()), Users::EMAIL_STATUS_UNCONFIRMED, DBField::NOT_NULL | DBField::INTERNAL),
00034 new DBFieldInt('tos_version', 0, DBFieldInt::UNSIGNED | DBField::NOT_NULL | DBField::INTERNAL),
00035 new DBFieldEnum('status', array_keys($this->get_allowed_status()), Users::STATUS_UNCONFIRMED, DBField::NOT_NULL | DBField::INTERNAL),
00036 ), $this->get_timestamp_field_declarations()
00037 ),
00038 'id'
00039 );
00040 }
00041
00042
00043
00044
00045
00046
00047 public function get_roles() {
00048 return $this->get_from_cache('userroles', 'do_get_roles');
00049 }
00050
00051
00052
00053
00054
00055
00056 protected function do_get_roles($params) {
00057 return UserRoles::get_for_user($this->id);
00058 }
00059
00060
00061
00062
00063
00064
00065 protected function get_allowed_status() {
00066 return Users::get_statuses();
00067 }
00068
00069
00070
00071
00072
00073
00074
00075 public function create_token($creator, $data) {
00076 $src = '';
00077 $src .= $this->email;
00078 $src .= $this->password;
00079 $src .= $creator;
00080 $src .= $this->id;
00081 if (is_array($data)) {
00082 $data = Arr::implode('ยง', $data, '~');
00083 }
00084 $src .= $data;
00085 $src .= $this->get_creation_date();
00086 $src .= $this->name;
00087 return sha1($src);
00088 }
00089
00090
00091
00092
00093
00094
00095 public function confirmed_tos() {
00096 return $this->tos_version >= Config::get_value(ConfigUsermanagement::TOS_VERSION);
00097 }
00098
00099
00100
00101
00102
00103
00104 public function confirmed_email() {
00105 return $this->emailstatus == Users::EMAIL_STATUS_CONFIRMED;
00106 }
00107
00108
00109
00110
00111 public function password_match($pwd) {
00112 $algo = Users::create_hash_algorithm($this->hash_type);
00113 return $algo->check($pwd, $this->password);
00114 }
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125 public function get_title() {
00126 return $this->name;
00127 }
00128
00129
00130
00131
00132
00133
00134 public function get_description() {
00135 return '';
00136 }
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147 public function set_status($status) {
00148 $this->status = $status;
00149 }
00150
00151
00152
00153
00154
00155
00156 public function get_status() {
00157 return $this->status;
00158 }
00159
00160
00161
00162
00163
00164
00165 public function is_active() {
00166 return $this->status == Users::STATUS_ACTIVE;
00167 }
00168
00169
00170
00171
00172
00173
00174 public function is_unconfirmed() {
00175 return $this->status == Users::STATUS_UNCONFIRMED;
00176 }
00177
00178
00179
00180
00181
00182
00183 public function is_deleted() {
00184 return $this->status == Users::STATUS_DELETED;
00185 }
00186
00187
00188
00189
00190
00191
00192 public function is_disabled() {
00193 return $this->status == Users::STATUS_DISABLED;
00194 }
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206 public function has_role($role) {
00207 $ret = false;
00208 $my_roles = $this->get_role_names();
00209 foreach(Arr::force($role) as $check) {
00210 if (array_key_exists($check, $my_roles)) {
00211 $ret = true;
00212 break;
00213 }
00214 }
00215 return $ret;
00216 }
00217
00218
00219
00220
00221
00222
00223 public function get_role_names() {
00224 return $this->get_from_cache('userrolenames', 'do_get_role_names');
00225 }
00226
00227
00228
00229
00230
00231
00232 protected function do_get_role_names($params) {
00233 $ret = array();
00234 foreach($this->get_roles() as $role) {
00235 $t = trim($role->name);
00236 $ret[$t] = $t;
00237 }
00238 return $ret;
00239 }
00240
00241
00242
00243
00244
00245 public function confirm() {
00246 $this->status = Users::STATUS_ACTIVE;
00247 }
00248
00249
00250
00251
00252
00253
00254
00255
00256 public function get_filters() {
00257 return array(
00258 new DBFilterGroup(
00259 'status',
00260 tr('Status'),
00261 array(
00262 'unconfirmed' => new DBFilterColumn('users.status', Users::STATUS_UNCONFIRMED, tr('Unconfirmed', 'users')),
00263 'disabled' => new DBFilterColumn('users.status', Users::STATUS_DISABLED, tr('Disabled', 'users')),
00264 'deleted' => new DBFilterColumn('users.status', Users::STATUS_DELETED, tr('Deleted', 'users')),
00265 'active' => new DBFilterColumn('users.status', Users::STATUS_ACTIVE, tr('Active', 'users')),
00266 )
00267 ),
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280 );
00281 }
00282
00283
00284
00285
00286 public function get_sortable_columns() {
00287 return array(
00288 'name' => new DBSortColumn('name', tr('Name', 'users'), DBSortColumn::TYPE_TEXT),
00289 'email' => new DBSortColumn('email', tr('E-mail', 'users'), DBSortColumn::TYPE_TEXT),
00290 'creationdate' => new DBSortColumn('creationdate', tr('Registered since', 'users'), DBSortColumn::TYPE_DATE)
00291 );
00292 }
00293
00294
00295
00296
00297 public function get_sort_default_column() {
00298 return 'name';
00299 }
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313 protected function get_actions_for_context($context, $user, $params) {
00314 $ret = array();
00315 $ret['edit'] = tr('Edit user', 'users');
00316
00317 $arrStates = array(
00318 Users::STATUS_ACTIVE,
00319 Users::STATUS_DISABLED,
00320 Users::STATUS_DELETED
00321 );
00322 foreach($arrStates as $state) {
00323 $cmd = 'status[' . $state . ']';
00324 $desc = tr('Set ' . $state);
00325 $ret[$cmd] = $desc;
00326 }
00327 return $ret;
00328 }
00329 }