public function post()
 {
     $post = Input::all();
     $validator = User::validate($post);
     $userId = $post['id'];
     if ($validator->fails()) {
         return Redirect::to('usuarios/' . $userId)->withErrors($validator)->withInput();
     } else {
         $user = self::__checkExistence($userId);
         if (!$userId) {
             $user = new User();
         }
         $user->name = $post['name'];
         $user->last_name = $post['last_name'];
         $user->display_name = $post['display_name'];
         $user->is_approved = $post['approved'] == '' ? null : ($post['approved'] == 'approved' ? true : false);
         $user->profile_type = $post['profile_type'];
         $user->email = $post['email'];
         $user->mobile = $post['mobile'];
         $sendMail = false;
         if (!$user->id) {
             //new user
             $passwordText = str_random(8);
             $passwordHashed = Hash::make($passwordText);
             $sendMail = true;
             $user->password = $passwordHashed;
             $user->is_approved = true;
         }
         //save user data
         $user->save();
         if ($post['status'] == 'inactive') {
             $user->delete();
         } else {
             if ($user->trashed()) {
                 $user->restore();
             }
         }
         if ($sendMail) {
             $mailView = 'emails.newPassword';
             $subject = 'Bienvenido a CloudInventory';
             Mail::send($mailView, array('user' => $user->email, 'password' => $passwordText), function ($message) use($user, $subject) {
                 $message->to($user->email, $user->display_name)->subject($subject);
             });
             Session::flash('warning', 'Su contraseña fue enviada a su correo electrónico.');
         }
         Session::flash('success', 'Usuario guardado correctamente.');
         return Redirect::to('usuarios');
     }
 }
 /**
  * undocumented function
  *
  * @return void
  * @access public
  */
 function admin_recalc_cache($type = null, $offset = 0)
 {
     $limit = 300;
     $this->set(compact('type', 'offset', 'limit'));
     if (empty($type)) {
         return;
     }
     ini_set('max_execution_time', 1800);
     ini_set('memory_limit', '1024M');
     if ($type == 'clear_file') {
         Cache::clear();
         clearCache();
         $this->Message->add(__('The cache has been cleared.', true), 'ok');
     }
     if ($type == 'session_restore') {
         User::restore();
     }
     if ($done) {
         $this->Message->add(__('The counter caches have been rebuilt.', true), 'ok');
     }
     $this->set(compact('done'));
 }
         Event::log($newID, "users", 4, "setup", sprintf(__('%1$s adds the item %2$s'), $_SESSION["glpiname"], $_POST["name"]));
         if ($_SESSION['glpibackcreated']) {
             Html::redirect($user->getFormURL() . "?id=" . $newID);
         }
     }
     Html::back();
 } else {
     if (isset($_POST["delete"])) {
         $user->check($_POST['id'], DELETE);
         $user->delete($_POST);
         Event::log($_POST["id"], "users", 4, "setup", sprintf(__('%s deletes an item'), $_SESSION["glpiname"]));
         $user->redirectToList();
     } else {
         if (isset($_POST["restore"])) {
             $user->check($_POST['id'], PURGE);
             $user->restore($_POST);
             Event::log($_POST["id"], "users", 4, "setup", sprintf(__('%s restores an item'), $_SESSION["glpiname"]));
             $user->redirectToList();
         } else {
             if (isset($_POST["purge"])) {
                 $user->check($_POST['id'], PURGE);
                 $user->delete($_POST, 1);
                 Event::log($_POST["id"], "users", 4, "setup", sprintf(__('%s purges an item'), $_SESSION["glpiname"]));
                 $user->redirectToList();
             } else {
                 if (isset($_POST["force_ldap_resynch"])) {
                     Session::checkRight('user', User::UPDATEAUTHENT);
                     $user->getFromDB($_POST["id"]);
                     AuthLdap::ldapImportUserByServerId(array('method' => AuthLDAP::IDENTIFIER_LOGIN, 'value' => $user->fields["name"]), true, $user->fields["auths_id"], true);
                     Html::back();
                 } else {
Example #4
0
 function index()
 {
     $deleted = array();
     //用email建立user1
     //$user1 = $this->user->create($this->url, 'name', $this->user_name);
     $user1 = $this->user->create_user($this->url, $this->user_email);
     //檢查user1的name
     $this->unit->run($user1->get_name(), 'puddingchen.35', '檢查user1的name');
     $deleted[] = $user1->get_id();
     //檢查$user1的domain
     $domain1 = $user1->get_domain();
     $this->unit->run($domain1->get_host(), $this->host, '檢查$user1的domain');
     //測試密碼是否如期一樣地編碼
     $this->unit->run($user1->_crypt_password($this->password), $this->crypt_password, '測試密碼是否如期一樣地編碼');
     //設定user1的密碼
     $user1->set_password($this->password);
     $user1->update();
     $user_another = $this->user->create_user($this->url, $this->user_email2);
     $this->unit->run_false($user1->get_id(), $user_another->get_id(), '建立user_another,看看是否是不同人');
     //find user2,用name跟password來找
     $user2 = $this->user->find_user($this->url, $this->user_email, $this->password);
     //檢查user2的name
     $this->unit->run($user2->get_name(), $this->user_name, 'find user2,用name跟password來找 檢查user2的name');
     //用錯誤的密碼去找找看
     $user21 = $this->user->find_user($this->url, $this->user_name, 'error_password');
     $this->unit->run($user21, NULL, '用錯誤的密碼去找找看');
     //用email建立user3
     $user3 = $this->user->create_user($this->url, $this->user_email);
     $deleted[] = $user3->get_id();
     //檢查user3的name
     $this->unit->run($user3->get_name(), 'puddingchen.35', '用email建立user3 檢查user3的name');
     //測試search吧
     $users = $this->user->search($this->url, $this->keyword);
     $this->unit->run(count($users) > 0, TRUE, '測試Search,應該要找到東西才是');
     $this->unit->run($users[0]->get_name(), $this->user_name, '測試Search,看第一個是不是剛剛建立的');
     $users = $this->user->search($this->url, $this->not_keyword);
     $this->unit->run(count($users), 0, '測試錯誤的Search,應該找不到東西才是');
     //刪除user1
     $user1_id = $user1->get_id();
     $user1->delete();
     //檢查user1的name
     $this->unit->run($user1->get_name(), NULL, '刪除user1 檢查user1的name');
     $this->db->where('user_id', $user1_id);
     $this->db->where('deleted', 'FALSE');
     $count = $this->db->count_all_results('user');
     $this->unit->run($count, 0, '用資料庫來測試看看');
     //恢復user1
     $user1 = new User($user1_id);
     $user1->restore();
     $this->unit->run($user1->get_name(), $this->user_name, '恢復user1 檢查user1的name');
     $this->db->where('user_id', $user1_id);
     $this->db->where('deleted', 'FALSE');
     $count = $this->db->count_all_results('user');
     $this->unit->run($count, 1, '恢復user1, 用資料庫來測試看看');
     $user1->delete();
     //用user2去find看看
     $user2 = $this->user->find_user($this->url, $this->user_name);
     $this->unit->run($user2, NULL, '刪除user1 用user2去find看看');
     //刪除user3
     $user3->delete();
     //檢查user3的email
     $this->unit->run($user3->get_email(), NULL, '刪除user3 檢查user3的email');
     $user1 = $this->user->create_user($this->url, $this->user_email);
     $count1 = $this->db->where('deleted', 'TRUE')->count_all_results('user');
     $this->unit->run($count1, 1, '刪除之後,資料是否還在?');
     $count2 = $this->db->where('deleted', 'FALSE')->count_all_results('user');
     $this->unit->run($count1, 1, '刪除之後又新增,是否有新增的資料?');
     $count3 = $this->db->count_all_results('user');
     $this->unit->run($count1 + $count2, $count3, '刪除與新增的資料相加,是否等於全部資料表的筆數?');
     //        $this->unit->run($test_result,
     //                $expected_result,
     //                $test_name);
     //        $this->load->database();
     //        $domain_count = $this->db->count_all_results('domain', array('host'=> 'http://www.plurk.com/'));
     //        $this->unit->run($domain_count < 2
     //                , TRUE
     //                , '確認沒有多餘的Domain');
     //
     //        //單元測試之後不留下垃圾!
     //        foreach ($deleted AS $d)
     //        {
     //            $this->db->delete(array('domain2user', 'user'), array('user_id'=> $d));
     //        }
     unit_test_report($this);
 }