public function addUser($ldap) { $profile = Yii::$app->userHelpers->getUserProfileFromHr($ldap->citizen); $profile['moo'] = empty($profile['moo']) ? '' : ' หมู่ ' . $profile['moo']; $profile['soi'] = empty($profile['soi']) ? '' : ' ซอย ' . $profile['soi']; $profile['street'] = empty($profile['street']) ? '' : ' ถ.' . $profile['street']; $profile['district'] = empty($profile['district']) ? '' : ' ต.' . $profile['district']; $profile['amphur'] = empty($profile['amphur']) ? '' : ' อ.' . $profile['amphur']; $profile['province'] = empty($profile['province']) ? '' : ' จ.' . $profile['province']; $profile['zipcode'] = empty($profile['zipcode']) ? '' : ' ' . $profile['zipcode']; $user = new User(); $user->id = $profile['id']; $user->username = $this->username; $user->email = "{$this->username}@kku.ac.th"; $user->password_hash = Password::hash($this->password); $user->save(false); $connection = new Connection(Yii::$app->db); $connection->createCommand('INSERT INTO auth_assignment VALUES(:item_name, :user_id, :created_at)', [':item_name' => 'User', ':user_id' => $profile['id'], ':created_at' => time()])->execute(); $admins = Yii::$app->getModule('user')->admins; if (in_array($ldap->username, $admins)) { $connection->createCommand('INSERT INTO auth_assignment VALUES(:item_name, :user_id, :created_at)', [':item_name' => 'Administrator', ':user_id' => $profile['id'], ':created_at' => time()])->execute(); } $connection->createCommand('UPDATE profile SET name = :name, address = :address, phone = :phone, faculty_id = :faculty_id, position_id = :position_id, position_type_id = :position_type_id, level_id = :level_id, division_id = :division_id WHERE user_id = :user_id ', [':name' => "{$profile['title']}{$profile['firstname']} {$profile['lastname']}", ':address' => "{$profile['homeadd']}{$profile['moo']}{$profile['soi']}{$profile['street']}{$profile['district']}{$profile['amphur']}{$profile['province']}{$profile['zipcode']}", ':phone' => isset($profile['telephone']) ? $profile['telephone'] : null, ':faculty_id' => isset($profile['faculty_id']) ? $profile['faculty_id'] : Yii::$app->mappingHelpers->mapFaculty($profile['faculty'])['id'], ':position_id' => isset($profile['position_id']) ? $profile['position_id'] : Yii::$app->mappingHelpers->mapPosition($profile['posi'])['id'], ':position_type_id' => isset($profile['position_type_id']) ? $profile['position_type_id'] : Yii::$app->mappingHelpers->mapPositionType($profile['positype'])['id'], ':level_id' => isset($profile['level_id']) ? $profile['level_id'] : Yii::$app->mappingHelpers->mapLevel($profile['level'])['id'], ':division_id' => isset($profile['division_id']) ? $profile['division_id'] : Yii::$app->mappingHelpers->mapDivision($profile['division'])['id'], ':user_id' => $profile['id']])->execute(); }
/** * Validates form and logs the user in. * * @return bool whether the user is logged in successfully */ public function login() { if ($this->validate()) { if (Yii::$app->getUser()->login($this->user, $this->rememberMe ? $this->module->rememberFor : 0)) { UserLog::log("login-success"); $this->user->last_login = gmdate("Y-m-d H:i:s"); $this->user->save(); return true; } else { return false; } } else { $message = ''; foreach ($this->errors['login'] as $error) { $message .= $error . "\n"; } UserLog::log("login-failure", $message); return false; } }