00001 <?php
00002
00003
00004
00005
00006
00007
00008 class UpdateUsersBaseCommand extends CommandChain {
00009
00010
00011
00012 protected function do_execute() {
00013 $ret = new Status();
00014
00015 $params = $this->get_params();
00016 $user = $this->get_instance();
00017
00018
00019 $cmd_validate = CommandsFactory::create_command($user, 'validate', $params);
00020 $ret->merge($cmd_validate->execute());
00021
00022 if ($ret->is_ok()) {
00023
00024 $this->check_for_email_confirmation($user, $params);
00025
00026
00027 $this->hash_password($user, $params);
00028 $this->check_for_pwd_confirmation($user, $params);
00029
00030
00031 Load::commands('generics/update');
00032 $this->append(new UpdateCommand($user, $params));
00033
00034 $ret->merge($this->link_roles($user, $params));
00035 }
00036 return $ret;
00037 }
00038
00039
00040
00041
00042
00043
00044 protected function hash_password($user, &$params) {
00045 $pwd = Arr::get_item($params, 'password', '');
00046 if (!empty($pwd)) {
00047 $params['password'] = Users::create_hash($pwd, $user->hash_type);
00048 }
00049 else {
00050 unset($params['password']);
00051 }
00052 }
00053
00054
00055
00056
00057
00058
00059 protected function check_for_email_confirmation($user, &$params) {
00060 $email = Arr::get_item($params, 'email', $user->email);
00061 if (!Users::current_has_role(USER_ROLE_ADMIN)) {
00062
00063 if ($user->email !== $email) {
00064 $this->send_email_notification($user, $email);
00065 }
00066 unset($params['email']);
00067 }
00068 }
00069
00070
00071
00072
00073
00074
00075 protected function check_for_pwd_confirmation($user, &$params) {
00076 $pwd = Arr::get_item($params, 'password', $user->password);
00077 if (!Users::current_has_role(USER_ROLE_ADMIN)) {
00078
00079 if ($user->password !== $pwd) {
00080 $this->send_pwd_notification($user, $pwd);
00081 }
00082 unset($params['password']);
00083 }
00084 }
00085
00086
00087
00088
00089
00090
00091 protected function send_email_notification($user, $email) {
00092
00093 $params = array(
00094 'id_item' => $user->id,
00095 'action' => 'changeemail',
00096 'data' => $email
00097 );
00098 $this->append(CommandsFactory::create_command('confirmations', 'create', $params));
00099 }
00100
00101
00102
00103
00104
00105
00106 protected function send_pwd_notification($user, $pwd) {
00107
00108 $params = array(
00109 'id_item' => $user->id,
00110 'action' => 'changepassword',
00111 'data' => $pwd
00112 );
00113 $this->append(CommandsFactory::create_command('confirmations', 'create', $params));
00114 }
00115
00116
00117
00118
00119
00120
00121
00122
00123 protected function link_roles($user, $params) {
00124 $ret = new Status();
00125 $roles = Arr::get_item($params, 'roles', false);
00126 if ($roles === false) {
00127 return $ret;
00128 }
00129
00130 if (empty($roles)) {
00131 $ret->append(tr('You must assign at least one role', 'users'));
00132 return $ret;
00133 }
00134
00135
00136 $dao = new DAOUsers2userroles();
00137 $dao->add_where('id_user', '=', $user->id);
00138 $sql = $dao->create_delete_query(DAOUsers2userroles::WHERE_ONLY)->get_sql();
00139 Load::commands('generics/execute.sql');
00140 $this->append(new ExecuteSqlCommand($sql));
00141
00142
00143 foreach($roles as $role) {
00144 $params_link = array(
00145 'id_user' => $user->id,
00146 'id_role' => $role
00147 );
00148 $this->append(CommandsFactory::create_command('users2userroles', 'create', $params_link));
00149 }
00150 return $ret;
00151 }
00152
00153 }