00001 <?php
00002
00003
00004
00005
00006
00007
00008 class CreateUsersBaseCommand extends CommandChain {
00009
00010
00011
00012 protected function do_execute() {
00013 $ret = new Status();
00014
00015 $params = $this->preprocess_params($this->get_params());
00016
00017
00018 $cmd_validate = CommandsFactory::create_command('users', 'validate', $params);
00019 $ret->merge($cmd_validate->execute());
00020
00021
00022 if ($ret->is_ok()) {
00023 Load::commands('generics/create');
00024 $cmd = new CreateCommand('users', $params);
00025 $ret->merge($cmd->execute());
00026 $this->set_result($cmd->get_result());
00027 }
00028
00029
00030 if ($ret->is_ok()) {
00031 $user = $this->get_result();
00032 $ret->merge($this->link_roles($user, $params));
00033 }
00034
00035 if ($ret->is_ok()) {
00036 $user = $this->get_result();
00037 $ret->merge($this->postprocess($user, $params));
00038 }
00039
00040 return $ret;
00041 }
00042
00043
00044
00045
00046
00047
00048
00049
00050 protected function link_roles($user, $params) {
00051 $ret = new Status();
00052
00053 $roles = Arr::force(Arr::get_item($params, 'roles', array()));
00054
00055 foreach($roles as $role) {
00056 $params_link = array(
00057 'id_user' => $user->id,
00058 'id_role' => $role
00059 );
00060 $this->append(CommandsFactory::create_command('users2userroles', 'create', $params_link));
00061 }
00062
00063 return $ret;
00064 }
00065
00066
00067
00068
00069
00070
00071
00072
00073 protected function postprocess($user, $params) {
00074 return new Status();
00075 }
00076
00077
00078
00079
00080
00081
00082
00083 protected function preprocess_params($params) {
00084 $params['tos_version'] = Config::get_value(ConfigUsermanagement::TOS_VERSION);
00085
00086 $params['hash_type'] = Config::get_value(ConfigUsermanagement::HASH_TYPE, 'md5');
00087 $this->preprocess_hash_password($params);
00088 $this->preprocess_check_roles($params);
00089 return $params;
00090 }
00091
00092
00093
00094
00095
00096
00097 protected function preprocess_hash_password(&$params) {
00098 $params['hash_type'] = Arr::get_item($params, 'hash_type', Config::get_value(ConfigUsermanagement::HASH_TYPE, 'md5'));
00099
00100 $pwd = Arr::get_item($params, 'password', '');
00101 if (!empty($pwd)) {
00102 $params['password'] = Users::create_hash($pwd, $params['hash_type']);
00103 }
00104 else {
00105 unset($params['password']);
00106 }
00107 }
00108
00109
00110
00111
00112
00113
00114 protected function preprocess_check_roles(&$params) {
00115
00116 $roles = Arr::force(Arr::get_item($params, 'roles', array()));
00117 if (count($roles) == 0) {
00118
00119 $role = UserRoles::get_by_name(Config::get_value(ConfigUsermanagement::DEFAULT_ROLE));
00120 if ($role) {
00121 $roles[] = $role->id;
00122 $params['roles'] = $roles;
00123 }
00124 }
00125 }
00126 }