From d167f31af0f2fc595673cc4637ce9bd6e4bc5a75 Mon Sep 17 00:00:00 2001 From: Aladin Alaily Date: Thu, 13 Aug 2015 15:06:04 -0400 Subject: [PATCH] Fixed indents --- app/controllers/admin/UsersController.php | 874 ++++++++++------------ 1 file changed, 401 insertions(+), 473 deletions(-) diff --git a/app/controllers/admin/UsersController.php b/app/controllers/admin/UsersController.php index 53c738684ac1..cda415ba4de2 100755 --- a/app/controllers/admin/UsersController.php +++ b/app/controllers/admin/UsersController.php @@ -1,4 +1,6 @@ - 'required|alpha_space|min:2', - 'last_name' => 'required|alpha_space|min:2', - 'location_id' => 'numeric', - 'username' => 'required|min:2|unique:users,username', - 'email' => 'email|unique:users,email', - 'password' => 'required|min:6', + 'first_name' => 'required|alpha_space|min:2', + 'last_name' => 'required|alpha_space|min:2', + 'location_id' => 'numeric', + 'username' => 'required|min:2|unique:users,username', + 'email' => 'email|unique:users,email', + 'password' => 'required|min:6', 'password_confirm' => 'required|min:6|same:password', ); @@ -51,8 +54,7 @@ class UsersController extends AdminController * * @return View */ - public function getIndex() - { + public function getIndex() { // Show the page return View::make('backend/users/index'); @@ -63,8 +65,7 @@ public function getIndex() * * @return View */ - public function getCreate() - { + public function getCreate() { // Get all the available groups $groups = Sentry::getGroupProvider()->findAll(); @@ -81,24 +82,23 @@ public function getCreate() $location_list = array('' => '') + Location::lists('name', 'id'); $manager_list = array('' => '') + DB::table('users') - ->select(DB::raw('concat(last_name,", ",first_name," (",username,")") as full_name, id')) - ->whereNull('deleted_at','and') - ->orderBy('last_name', 'asc') - ->orderBy('first_name', 'asc') - ->lists('full_name', 'id'); - - /*echo '
';
-		print_r($userPermissions);
-		echo '
'; - exit; - */ + ->select(DB::raw('concat(last_name,", ",first_name," (",username,")") as full_name, id')) + ->whereNull('deleted_at', 'and') + ->orderBy('last_name', 'asc') + ->orderBy('first_name', 'asc') + ->lists('full_name', 'id'); + + /* echo '
';
+          print_r($userPermissions);
+          echo '
'; + exit; + */ // Show the page return View::make('backend/users/edit', compact('groups', 'userGroups', 'permissions', 'userPermissions')) - ->with('location_list',$location_list) - ->with('manager_list',$manager_list) - ->with('user',new User); - + ->with('location_list', $location_list) + ->with('manager_list', $manager_list) + ->with('user', new User); } /** @@ -106,34 +106,32 @@ public function getCreate() * * @return Redirect */ - public function postCreate() - { + public function postCreate() { // Create a new validator instance from our validation rules $validator = Validator::make(Input::all(), $this->validationRules); - $permissions = Input::get('permissions', array()); - $this->decodePermissions($permissions); + $permissions = Input::get('permissions', array()); + $this->decodePermissions($permissions); app('request')->request->set('permissions', $permissions); // If validation fails, we'll exit the operation now. if ($validator->fails()) { // Ooops.. something went wrong - return Redirect::back()->withInput()->withErrors($validator)->with('permissions',$permissions); + return Redirect::back()->withInput()->withErrors($validator)->with('permissions', $permissions); } try { // We need to reverse the UI specific logic for our // permissions here before we create the user. - // Get the inputs, with some exceptions - $inputs = Input::except('csrf_token', 'password_confirm', 'groups','email_user'); + $inputs = Input::except('csrf_token', 'password_confirm', 'groups', 'email_user'); - // @TODO: Figure out WTF I need to do this. - if ($inputs['manager_id']=='') { - unset($inputs['manager_id']); + // @TODO: Figure out WTF I need to do this. + if ($inputs['manager_id'] == '') { + unset($inputs['manager_id']); } - if ($inputs['location_id']=='') { - unset($inputs['location_id']); + if ($inputs['location_id'] == '') { + unset($inputs['location_id']); } // Was the user created? @@ -151,20 +149,20 @@ public function postCreate() // Redirect to the new user page //return Redirect::route('update/user', $user->id)->with('success', $success); - if ((Input::get('email_user')==1) && (Input::has('email'))) { - // Send the credentials through email + if ((Input::get('email_user') == 1) && (Input::has('email'))) { + // Send the credentials through email - $data = array(); - $data['email'] = e(Input::get('email')); + $data = array(); + $data['email'] = e(Input::get('email')); $data['username'] = e(Input::get('username')); - $data['first_name'] = e(Input::get('first_name')); - $data['password'] = e(Input::get('password')); + $data['first_name'] = e(Input::get('first_name')); + $data['password'] = e(Input::get('password')); - Mail::send('emails.send-login', $data, function ($m) use ($user) { - $m->to($user->email, $user->first_name . ' ' . $user->last_name); - $m->subject('Welcome ' . $user->first_name); - }); - } + Mail::send('emails.send-login', $data, function ($m) use ($user) { + $m->to($user->email, $user->first_name . ' ' . $user->last_name); + $m->subject('Welcome ' . $user->first_name); + }); + } return Redirect::route('users')->with('success', $success); @@ -189,8 +187,7 @@ public function postCreate() return Redirect::route('create/user')->withInput()->with('error', $error); } - public function store() - { + public function store() { // Create a new validator instance from our validation rules $validator = Validator::make(Input::all(), $this->validationRules); $permissions = Input::get('permissions', array()); @@ -200,36 +197,36 @@ public function store() // If validation fails, we'll exit the operation now. if ($validator->fails()) { // Ooops.. something went wrong - return JsonResponse::create(["error" => "Failed validation: ".print_r($validator->messages()->all('
  • :message
  • '),true)],500); } + return JsonResponse::create(["error" => "Failed validation: " . print_r($validator->messages()->all('
  • :message
  • '), true)], 500); + } try { // We need to reverse the UI specific logic for our // permissions here before we create the user. - // Get the inputs, with some exceptions - $inputs = Input::except('csrf_token', 'password_confirm', 'groups','email_user'); + $inputs = Input::except('csrf_token', 'password_confirm', 'groups', 'email_user'); $inputs['activated'] = true; // @TODO: Figure out WTF I need to do this. - /*if ($inputs['manager_id']=='') { + /* if ($inputs['manager_id']=='') { unset($inputs['manager_id']); - }*/ + } */ - /*if ($inputs['location_id']=='') { + /* if ($inputs['location_id']=='') { unset($inputs['location_id']); - }*/ + } */ // Was the user created? if ($user = Sentry::getUserProvider()->create($inputs)) { - if (Input::get('email_user')==1) { - // Send the credentials through email + if (Input::get('email_user') == 1) { + // Send the credentials through email - $data = array(); - $data['email'] = e(Input::get('email')); - $data['first_name'] = e(Input::get('first_name')); - $data['password'] = e(Input::get('password')); + $data = array(); + $data['email'] = e(Input::get('email')); + $data['first_name'] = e(Input::get('first_name')); + $data['password'] = e(Input::get('password')); Mail::send('emails.send-login', $data, function ($m) use ($user) { $m->to($user->email, $user->first_name . ' ' . $user->last_name); @@ -240,15 +237,12 @@ public function store() return JsonResponse::create($user); } else { - return JsonResponse::create(["error" => "Couldn't save User"],500); + return JsonResponse::create(["error" => "Couldn't save User"], 500); } - - - } catch (Exception $e) { - // Redirect to the user creation page - return JsonResponse::create(["error" => "Failed validation: ".print_r($validator->messages()->all('
  • :message
  • '),true)],500); + // Redirect to the user creation page + return JsonResponse::create(["error" => "Failed validation: " . print_r($validator->messages()->all('
  • :message
  • '), true)], 500); } } @@ -258,8 +252,7 @@ public function store() * @param int $id * @return View */ - public function getEdit($id = null) - { + public function getEdit($id = null) { try { // Get the user information $user = Sentry::getUserProvider()->findById($id); @@ -280,13 +273,12 @@ public function getEdit($id = null) $location_list = array('' => '') + Location::lists('name', 'id'); $manager_list = array('' => 'Select a User') + DB::table('users') - ->select(DB::raw('concat(last_name,", ",first_name," (",email,")") as full_name, id')) - ->whereNull('deleted_at') - ->where('id','!=',$id) - ->orderBy('last_name', 'asc') - ->orderBy('first_name', 'asc') - ->lists('full_name', 'id'); - + ->select(DB::raw('concat(last_name,", ",first_name," (",email,")") as full_name, id')) + ->whereNull('deleted_at') + ->where('id', '!=', $id) + ->orderBy('last_name', 'asc') + ->orderBy('first_name', 'asc') + ->lists('full_name', 'id'); } catch (UserNotFoundException $e) { // Prepare the error message $error = Lang::get('admin/users/message.user_not_found', compact('id')); @@ -297,8 +289,8 @@ public function getEdit($id = null) // Show the page return View::make('backend/users/edit', compact('user', 'groups', 'userGroups', 'permissions', 'userPermissions')) - ->with('location_list',$location_list) - ->with('manager_list',$manager_list); + ->with('location_list', $location_list) + ->with('manager_list', $manager_list); } /** @@ -307,8 +299,7 @@ public function getEdit($id = null) * @param int $id * @return Redirect */ - public function postEdit($id = null) - { + public function postEdit($id = null) { // We need to reverse the UI specific logic for our // permissions here before we update the user. $permissions = Input::get('permissions', array()); @@ -333,7 +324,7 @@ public function postEdit($id = null) //Check if username is the same then unset validationRules if (Input::get('username') == $user->username) { - unset($this->validationRules['username']); + unset($this->validationRules['username']); } //Check if email is the same then unset validationRules @@ -342,7 +333,7 @@ public function postEdit($id = null) } // Do we want to update the user password? - if ( ! $password = Input::get('password')) { + if (!$password = Input::get('password')) { unset($this->validationRules['password']); unset($this->validationRules['password_confirm']); #$this->validationRules['password'] = 'required|between:3,32'; @@ -361,36 +352,36 @@ public function postEdit($id = null) try { // Update the user - $user->first_name = Input::get('first_name'); - $user->last_name = Input::get('last_name'); - $user->username = Input::get('username'); - $user->email = Input::get('email'); - $user->employee_num = Input::get('employee_num'); - $user->activated = Input::get('activated', $user->activated); - $user->permissions = Input::get('permissions'); - $user->jobtitle = Input::get('jobtitle'); - $user->phone = Input::get('phone'); - $user->location_id = Input::get('location_id'); - $user->manager_id = Input::get('manager_id'); - $user->notes = Input::get('notes'); + $user->first_name = Input::get('first_name'); + $user->last_name = Input::get('last_name'); + $user->username = Input::get('username'); + $user->email = Input::get('email'); + $user->employee_num = Input::get('employee_num'); + $user->activated = Input::get('activated', $user->activated); + $user->permissions = Input::get('permissions'); + $user->jobtitle = Input::get('jobtitle'); + $user->phone = Input::get('phone'); + $user->location_id = Input::get('location_id'); + $user->manager_id = Input::get('manager_id'); + $user->notes = Input::get('notes'); if ($user->manager_id == "") { $user->manager_id = NULL; } if ($user->location_id == "") { - $user->location_id = NULL; + $user->location_id = NULL; } // Do we want to update the user password? - if (($password) && (!Config::get('app.lock_passwords'))) { + if (($password) && (!Config::get('app.lock_passwords'))) { $user->password = $password; } // Do we want to update the user email? if (!Config::get('app.lock_passwords')) { - $user->email = Input::get('email'); + $user->email = Input::get('email'); } // Get the current user groups @@ -401,24 +392,24 @@ public function postEdit($id = null) // Groups comparison between the groups the user currently // have and the groups the user wish to have. - $groupsToAdd = array_diff($selectedGroups, $userGroups); + $groupsToAdd = array_diff($selectedGroups, $userGroups); $groupsToRemove = array_diff($userGroups, $selectedGroups); - if (!Config::get('app.lock_passwords')) { + if (!Config::get('app.lock_passwords')) { - // Assign the user to groups - foreach ($groupsToAdd as $groupId) { - $group = Sentry::getGroupProvider()->findById($groupId); - $user->addGroup($group); - } + // Assign the user to groups + foreach ($groupsToAdd as $groupId) { + $group = Sentry::getGroupProvider()->findById($groupId); + $user->addGroup($group); + } - // Remove the user from groups - foreach ($groupsToRemove as $groupId) { - $group = Sentry::getGroupProvider()->findById($groupId); + // Remove the user from groups + foreach ($groupsToRemove as $groupId) { + $group = Sentry::getGroupProvider()->findById($groupId); - $user->removeGroup($group); - } - } + $user->removeGroup($group); + } + } // Was the user updated? if ($user->save()) { @@ -445,8 +436,7 @@ public function postEdit($id = null) * @param int $id * @return Redirect */ - public function getDelete($id = null) - { + public function getDelete($id = null) { try { // Get user information $user = Sentry::getUserProvider()->findById($id); @@ -470,13 +460,13 @@ public function getDelete($id = null) if (count($user->assets) > 0) { // Redirect to the user management page - return Redirect::route('users')->with('error', 'This user still has '.count($user->assets).' assets associated with them.'); + return Redirect::route('users')->with('error', 'This user still has ' . count($user->assets) . ' assets associated with them.'); } if (count($user->licenses) > 0) { // Redirect to the user management page - return Redirect::route('users')->with('error', 'This user still has '.count($user->licenses).' licenses associated with them.'); + return Redirect::route('users')->with('error', 'This user still has ' . count($user->licenses) . ' licenses associated with them.'); } // Delete the user @@ -489,7 +479,7 @@ public function getDelete($id = null) return Redirect::route('users')->with('success', $success); } catch (UserNotFoundException $e) { // Prepare the error message - $error = Lang::get('admin/users/message.user_not_found', compact('id' )); + $error = Lang::get('admin/users/message.user_not_found', compact('id')); // Redirect to the user management page return Redirect::route('users')->with('error', $error); @@ -499,30 +489,27 @@ public function getDelete($id = null) public function postBulkEdit() { if ((!Input::has('edit_user')) || (count(Input::has('edit_user')) == 0)) { - return Redirect::back()->with('error', 'No users selected'); - } else { + return Redirect::back()->with('error', 'No users selected'); + } else { $statuslabel_list = array('' => Lang::get('general.select_statuslabel')) + Statuslabel::orderBy('name', 'asc')->lists('name', 'id'); $user_raw_array = Input::get('edit_user'); $users = User::whereIn('id', $user_raw_array)->with('groups')->get(); - return View::make('backend/users/confirm-bulk-delete', compact('users','statuslabel_list')); - - } - + return View::make('backend/users/confirm-bulk-delete', compact('users', 'statuslabel_list')); + } } public function postBulkSave() { if ((!Input::has('edit_user')) || (count(Input::has('edit_user')) == 0)) { - return Redirect::back()->with('error', 'No users selected'); - + return Redirect::back()->with('error', 'No users selected'); } elseif ((!Input::has('status_id')) || (count(Input::has('status_id')) == 0)) { - return Redirect::route('users')->with('error', 'No status selected'); - } else { + return Redirect::route('users')->with('error', 'No status selected'); + } else { $user_raw_array = Input::get('edit_user'); $asset_array = array(); - if(($key = array_search(Sentry::getId(), $user_raw_array)) !== false) { + if (($key = array_search(Sentry::getId(), $user_raw_array)) !== false) { unset($user_raw_array[$key]); } @@ -546,12 +533,10 @@ public function postBulkSave() { $log = $logaction->logaction('checkin from'); $update_assets = Asset::whereIn('id', $asset_array)->update( - array( - 'status_id' => e(Input::get('status_id')), - 'assigned_to' => '', - )); - - + array( + 'status_id' => e(Input::get('status_id')), + 'assigned_to' => '', + )); } foreach ($accessories as $accessory) { @@ -566,9 +551,9 @@ public function postBulkSave() { $log = $logaction->logaction('checkin from'); $update_assets = DB::table('accessories_users')->whereIn('id', $accessory_array)->update( - array( - 'assigned_to' => '', - )); + array( + 'assigned_to' => '', + )); } @@ -578,8 +563,7 @@ public function postBulkSave() { } return Redirect::route('users')->with('error', 'An error has occurred'); - } - + } } /** @@ -588,8 +572,7 @@ public function postBulkSave() { * @param int $id * @return Redirect */ - public function getRestore($id = null) - { + public function getRestore($id = null) { try { // Get user information $user = Sentry::getUserProvider()->createModel()->withTrashed()->find($id); @@ -611,41 +594,36 @@ public function getRestore($id = null) } } - /** * Get user info for user view * * @param int $userId * @return View */ - public function getView($userId = null) - { - - $user = User::with('assets','assets.model','consumables','accessories','licenses','userloc')->withTrashed()->find($userId); + public function getView($userId = null) { - $userlog = $user->userlog->load('assetlog','consumablelog','assetlog.model','licenselog','accessorylog','userlog','adminlog'); + $user = User::with('assets', 'assets.model', 'consumables', 'accessories', 'licenses', 'userloc')->withTrashed()->find($userId); - if (isset($user->id)) { - return View::make('backend/users/view', compact('user','userlog')); - } else { - // Prepare the error message - $error = Lang::get('admin/users/message.user_not_found', compact('id' )); + $userlog = $user->userlog->load('assetlog', 'consumablelog', 'assetlog.model', 'licenselog', 'accessorylog', 'userlog', 'adminlog'); - // Redirect to the user management page - return Redirect::route('users')->with('error', $error); - } + if (isset($user->id)) { + return View::make('backend/users/view', compact('user', 'userlog')); + } else { + // Prepare the error message + $error = Lang::get('admin/users/message.user_not_found', compact('id')); + // Redirect to the user management page + return Redirect::route('users')->with('error', $error); + } } - /** * Unsuspend the given user. * * @param int $id * @return Redirect */ - public function getUnsuspend($id = null) - { + public function getUnsuspend($id = null) { try { // Get user information $user = Sentry::getUserProvider()->findById($id); @@ -676,15 +654,14 @@ public function getUnsuspend($id = null) return Redirect::route('users')->with('success', $success); } catch (UserNotFoundException $e) { // Prepare the error message - $error = Lang::get('admin/users/message.user_not_found', compact('id' )); + $error = Lang::get('admin/users/message.user_not_found', compact('id')); // Redirect to the user management page return Redirect::route('users')->with('error', $error); } } - public function getClone($id = null) - { + public function getClone($id = null) { // We need to reverse the UI specific logic for our // permissions here before we update the user. $permissions = Input::get('permissions', array()); @@ -698,7 +675,8 @@ public function getClone($id = null) $user = clone $user_to_clone; $user->first_name = ''; $user->last_name = ''; - $user->email = substr($user->email, ($pos = strpos($user->email, '@')) !== false ? $pos : 0);; + $user->email = substr($user->email, ($pos = strpos($user->email, '@')) !== false ? $pos : 0); + ; $user->id = null; // Get this user groups @@ -717,20 +695,19 @@ public function getClone($id = null) $location_list = array('' => '') + Location::lists('name', 'id'); $manager_list = array('' => 'Select a User') + DB::table('users') - ->select(DB::raw('concat(last_name,", ",first_name," (",email,")") as full_name, id')) - ->whereNull('deleted_at') - ->where('id','!=',$id) - ->orderBy('last_name', 'asc') - ->orderBy('first_name', 'asc') - ->lists('full_name', 'id'); - - // Show the page + ->select(DB::raw('concat(last_name,", ",first_name," (",email,")") as full_name, id')) + ->whereNull('deleted_at') + ->where('id', '!=', $id) + ->orderBy('last_name', 'asc') + ->orderBy('first_name', 'asc') + ->lists('full_name', 'id'); + + // Show the page return View::make('backend/users/edit', compact('groups', 'userGroups', 'permissions', 'userPermissions')) - ->with('location_list',$location_list) - ->with('manager_list',$manager_list) - ->with('user',$user) - ->with('clone_user',$user_to_clone); - + ->with('location_list', $location_list) + ->with('manager_list', $manager_list) + ->with('user', $user) + ->with('clone_user', $user_to_clone); } catch (UserNotFoundException $e) { // Prepare the error message $error = Lang::get('admin/users/message.user_not_found', compact('id')); @@ -741,268 +718,232 @@ public function getClone($id = null) } /** - * User import. - * - * @return View - */ - public function getImport() - { - // Get all the available groups - $groups = Sentry::getGroupProvider()->findAll(); - // Selected groups - $selectedGroups = Input::old('groups', array()); - // Get all the available permissions - $permissions = Config::get('permissions'); - $this->encodeAllPermissions($permissions); - // Selected permissions - $selectedPermissions = Input::old('permissions', array('superuser' => -1)); - $this->encodePermissions($selectedPermissions); - // Show the page - return View::make('backend/users/import', compact('groups', 'selectedGroups', 'permissions', 'selectedPermissions')); - } - - - /** - * User import form processing. - * - * @return Redirect - */ - public function postImport() - { - - if (! ini_get("auto_detect_line_endings")) { - ini_set("auto_detect_line_endings", '1'); - } - - $csv = Reader::createFromPath(Input::file('user_import_csv')); - $csv->setNewline("\r\n"); - - if (Input::get('has_headers')==1) { - $csv->setOffset(1); - } - - $duplicates = ''; - - $nbInsert = $csv->each(function ($row) use ($duplicates) { - - if (array_key_exists(2, $row)) { - - if (Input::get('activate')==1) { - $activated = '1'; - } else { - $activated = '0'; - } - - $pass = substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 10); - - - - try { - // Check if this email already exists in the system - $user = DB::table('users')->where('username', $row[2])->first(); - if ($user) { - $duplicates .= $row[2].', '; - } else { - - $newuser = array( - 'first_name' => $row[0], - 'last_name' => $row[1], - 'username' => $row[2], - 'email' => $row[3], - 'password' => $pass, - 'activated' => $activated, - 'location_id' => $row[4], - 'permissions' => '{"user":1}', - 'notes' => 'Imported user' - ); - - DB::table('users')->insert($newuser); - - $updateuser = Sentry::findUserByLogin($row[2]); - - // Update the user details - $updateuser->password = $pass; - - // Update the user - $updateuser->save(); - - - if (((Input::get('email_user')==1) && !Config::get('app.lock_passwords'))) { - // Send the credentials through email - if ($row[3]!='') { - $data = array(); - $data['username'] = $row[2]; - $data['first_name'] = $row[0]; - $data['password'] = $pass; - - if ($newuser['email']) { - Mail::send('emails.send-login', $data, function ($m) use ($newuser) { - $m->to($newuser['email'], $newuser['first_name'] . ' ' . $newuser['last_name']); - $m->subject('Welcome ' . $newuser['first_name']); - }); - } + * User import. + * + * @return View + */ + public function getImport() { + // Get all the available groups + $groups = Sentry::getGroupProvider()->findAll(); + // Selected groups + $selectedGroups = Input::old('groups', array()); + // Get all the available permissions + $permissions = Config::get('permissions'); + $this->encodeAllPermissions($permissions); + // Selected permissions + $selectedPermissions = Input::old('permissions', array('superuser' => -1)); + $this->encodePermissions($selectedPermissions); + // Show the page + return View::make('backend/users/import', compact('groups', 'selectedGroups', 'permissions', 'selectedPermissions')); + } - } - } - } + /** + * User import form processing. + * + * @return Redirect + */ + public function postImport() { + if (!ini_get("auto_detect_line_endings")) { + ini_set("auto_detect_line_endings", '1'); + } - } catch (Exception $e) { - echo 'Caught exception: ', $e->getMessage(), "\n"; - } - return true; - } + $csv = Reader::createFromPath(Input::file('user_import_csv')); + $csv->setNewline("\r\n"); - }); + if (Input::get('has_headers') == 1) { + $csv->setOffset(1); + } + $duplicates = ''; - return Redirect::route('users')->with('duplicates',$duplicates)->with('success', 'Success'); + $nbInsert = $csv->each(function ($row) use ($duplicates) { - } + if (array_key_exists(2, $row)) { + if (Input::get('activate') == 1) { + $activated = '1'; + } else { + $activated = '0'; + } - public function getDatatable($status = null) - { + $pass = substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 10); - $users = User::with('assets','accessories','consumables','licenses','manager','sentryThrottle','groups','userloc'); - switch ($status) { - case 'deleted': - $users->GetDeleted(); - break; - case '': - $users->GetNotDeleted(); - break; - } - $users = $users->orderBy('created_at', 'DESC')->get(); + try { + // Check if this email already exists in the system + $user = DB::table('users')->where('username', $row[2])->first(); + if ($user) { + $duplicates .= $row[2] . ', '; + } else { - $actions = new \Chumper\Datatable\Columns\FunctionColumn('actions', function ($users) - { - $action_buttons = ''; + $newuser = array( + 'first_name' => $row[0], + 'last_name' => $row[1], + 'username' => $row[2], + 'email' => $row[3], + 'password' => $pass, + 'activated' => $activated, + 'location_id' => $row[4], + 'permissions' => '{"user":1}', + 'notes' => 'Imported user' + ); + DB::table('users')->insert($newuser); - if ( ! is_null($users->deleted_at)) { - $action_buttons .= ' '; - } else { - if ($users->accountStatus()=='suspended') { - $action_buttons .= ' '; - } + $updateuser = Sentry::findUserByLogin($row[2]); - $action_buttons .= ' '; + // Update the user details + $updateuser->password = $pass; - if ((Sentry::getId() !== $users->id) && (!Config::get('app.lock_passwords'))) { - $action_buttons .= ' '; - } else { - $action_buttons .= ' '; - } - } - return $action_buttons; + // Update the user + $updateuser->save(); - }); + if (((Input::get('email_user') == 1) && !Config::get('app.lock_passwords'))) { + // Send the credentials through email + if ($row[3] != '') { + $data = array(); + $data['username'] = $row[2]; + $data['first_name'] = $row[0]; + $data['password'] = $pass; - return Datatable::collection($users) - ->addColumn('',function($users) - { - return '
    '; - }) - ->addColumn('name',function($users) - { - return ''.$users->fullName().''; - }) - - ->addColumn('email',function($users) - { - if ($users->email) { - return '
    '; - } else { - return ''; + if ($newuser['email']) { + Mail::send('emails.send-login', $data, function ($m) use ($newuser) { + $m->to($newuser['email'], $newuser['first_name'] . ' ' . $newuser['last_name']); + $m->subject('Welcome ' . $newuser['first_name']); + }); + } + } + } + } + } catch (Exception $e) { + echo 'Caught exception: ', $e->getMessage(), "\n"; } + return true; + } + }); - }) - ->addColumn('manager',function($users) - { - if ($users->manager) { - return ''.$users->manager->fullName().''; - } - }) + return Redirect::route('users')->with('duplicates', $duplicates)->with('success', 'Success'); + } - ->addColumn('location',function($users) - { - if ($users->userloc) { - return $users->userloc->name; - } - }) + public function getDatatable($status = null) { - ->addColumn('assets',function($users) - { - return $users->assets->count(); - }) + $users = User::with('assets', 'accessories', 'consumables', 'licenses', 'manager', 'sentryThrottle', 'groups', 'userloc'); - ->addColumn('licenses',function($users) - { - return $users->licenses->count(); - }) + switch ($status) { + case 'deleted': + $users->GetDeleted(); + break; + case '': + $users->GetNotDeleted(); + break; + } + + $users = $users->orderBy('created_at', 'DESC')->get(); - ->addColumn('accessories',function($users) - { - return $users->accessories->count(); - }) - ->addColumn('consumables',function($users) - { - return $users->consumables->count(); - }) + $actions = new \Chumper\Datatable\Columns\FunctionColumn('actions', function ($users) { + $action_buttons = ''; - ->addColumn('groups',function($users) - { - $group_names = ''; - foreach ($users->groups as $group) { - $group_names .= ''.$group->name.' '; - } - return $group_names; - }) + if (!is_null($users->deleted_at)) { + $action_buttons .= ' '; + } else { + if ($users->accountStatus() == 'suspended') { + $action_buttons .= ' '; + } + $action_buttons .= ' '; - ->addColumn($actions) - ->searchColumns('name','email','manager','activated','groups','location') - ->orderColumns('name','email','manager','activated', 'licenses','assets','accessories','consumables','groups','location') - ->make(); + if ((Sentry::getId() !== $users->id) && (!Config::get('app.lock_passwords'))) { + $action_buttons .= ' '; + } else { + $action_buttons .= ' '; + } + } + return $action_buttons; + }); - } + return Datatable::collection($users) + ->addColumn('', function($users) { + return '
    '; + }) + ->addColumn('name', function($users) { + return '' . $users->fullName() . ''; + }) + ->addColumn('email', function($users) { + if ($users->email) { + return '
    '; + } else { + return ''; + } + }) + ->addColumn('manager', function($users) { + if ($users->manager) { + return '' . $users->manager->fullName() . ''; + } + }) + ->addColumn('location', function($users) { + if ($users->userloc) { + return $users->userloc->name; + } + }) + ->addColumn('assets', function($users) { + return $users->assets->count(); + }) + ->addColumn('licenses', function($users) { + return $users->licenses->count(); + }) + ->addColumn('accessories', function($users) { + return $users->accessories->count(); + }) + ->addColumn('consumables', function($users) { + return $users->consumables->count(); + }) + ->addColumn('groups', function($users) { + $group_names = ''; + foreach ($users->groups as $group) { + $group_names .= '' . $group->name . ' '; + } + return $group_names; + }) + ->addColumn($actions) + ->searchColumns('name', 'email', 'manager', 'activated', 'groups', 'location') + ->orderColumns('name', 'email', 'manager', 'activated', 'licenses', 'assets', 'accessories', 'consumables', 'groups', 'location') + ->make(); + } /** - * Upload the file to the server - * - * @param int $assetId - * @return View - **/ - public function postUpload($userId = null) - { + * Upload the file to the server + * + * @param int $assetId + * @return View + * */ + public function postUpload($userId = null) { $user = User::find($userId); // the license is valid - $destinationPath = app_path().'/private_uploads'; + $destinationPath = app_path() . '/private_uploads'; if (isset($user->id)) { if (Input::hasFile('userfile')) { - foreach(Input::file('userfile') as $file) { + foreach (Input::file('userfile') as $file) { - $rules = array( - 'userfile' => 'required|mimes:png,gif,jpg,jpeg,doc,docx,pdf,txt,zip,rar|max:2000' - ); - $validator = Validator::make(array('userfile'=> $file), $rules); + $rules = array( + 'userfile' => 'required|mimes:png,gif,jpg,jpeg,doc,docx,pdf,txt,zip,rar|max:2000' + ); + $validator = Validator::make(array('userfile' => $file), $rules); - if($validator->passes()){ + if ($validator->passes()) { $extension = $file->getClientOriginalExtension(); - $filename = 'user-'.$user->id.'-'.str_random(8); - $filename .= '-'.Str::slug($file->getClientOriginalName()).'.'.$extension; + $filename = 'user-' . $user->id . '-' . str_random(8); + $filename .= '-' . Str::slug($file->getClientOriginalName()) . '.' . $extension; $upload_success = $file->move($destinationPath, $filename); //Log the deletion of seats to the log @@ -1011,27 +952,23 @@ public function postUpload($userId = null) $logaction->asset_type = 'user'; $logaction->user_id = Sentry::getUser()->id; $logaction->note = e(Input::get('notes')); - $logaction->checkedout_to = NULL; - $logaction->created_at = date("Y-m-d h:i:s"); - $logaction->filename = $filename; + $logaction->checkedout_to = NULL; + $logaction->created_at = date("Y-m-d h:i:s"); + $logaction->filename = $filename; $log = $logaction->logaction('uploaded'); } else { - return Redirect::back()->with('error', Lang::get('admin/users/message.upload.invalidfiles')); + return Redirect::back()->with('error', Lang::get('admin/users/message.upload.invalidfiles')); } - - } if ($upload_success) { return Redirect::back()->with('success', Lang::get('admin/users/message.upload.success')); } else { - return Redirect::back()->with('error', Lang::get('admin/users/message.upload.error')); + return Redirect::back()->with('error', Lang::get('admin/users/message.upload.error')); } - } else { - return Redirect::back()->with('error', Lang::get('admin/users/message.upload.nofiles')); + return Redirect::back()->with('error', Lang::get('admin/users/message.upload.nofiles')); } - } else { // Prepare the error message $error = Lang::get('admin/users/message.does_not_exist', compact('id')); @@ -1041,28 +978,26 @@ public function postUpload($userId = null) } } -/** - * Delete the associated file - * - * @param int $assetId - * @return View - **/ - public function getDeleteFile($userId = null, $fileId = null) - { + /** + * Delete the associated file + * + * @param int $assetId + * @return View + * */ + public function getDeleteFile($userId = null, $fileId = null) { $user = User::find($userId); - $destinationPath = app_path().'/private_uploads'; + $destinationPath = app_path() . '/private_uploads'; // the license is valid if (isset($user->id)) { $log = Actionlog::find($fileId); - $full_filename = $destinationPath.'/'.$log->filename; + $full_filename = $destinationPath . '/' . $log->filename; if (file_exists($full_filename)) { - unlink($destinationPath.'/'.$log->filename); + unlink($destinationPath . '/' . $log->filename); } $log->delete(); return Redirect::back()->with('success', Lang::get('admin/users/message.deletefile.success')); - } else { // Prepare the error message $error = Lang::get('admin/users/message.does_not_exist', compact('id')); @@ -1072,24 +1007,21 @@ public function getDeleteFile($userId = null, $fileId = null) } } - - /** - * Display/download the uploaded file - * - * @param int $assetId - * @return View - **/ - public function displayFile($userId = null, $fileId = null) - { + * Display/download the uploaded file + * + * @param int $assetId + * @return View + * */ + public function displayFile($userId = null, $fileId = null) { $user = User::find($userId); // the license is valid if (isset($user->id)) { - $log = Actionlog::find($fileId); - $file = $log->get_src(); - return Response::download($file); + $log = Actionlog::find($fileId); + $file = $log->get_src(); + return Response::download($file); } else { // Prepare the error message $error = Lang::get('admin/users/message.does_not_exist', compact('id')); @@ -1099,54 +1031,48 @@ public function displayFile($userId = null, $fileId = null) } } + /** + * LDAP import + * + * @author Aladin Alaily + * @return View + */ + public function getLDAP() { + // Get all the available groups + $groups = Sentry::getGroupProvider()->findAll(); + // Selected groups + $selectedGroups = Input::old('groups', array()); + // Get all the available permissions + $permissions = Config::get('permissions'); + $this->encodeAllPermissions($permissions); + // Selected permissions + $selectedPermissions = Input::old('permissions', array('superuser' => -1)); + $this->encodePermissions($selectedPermissions); + // Show the page + return View::make('backend/users/ldap', compact('groups', 'selectedGroups', 'permissions', 'selectedPermissions')); + } - - - /** - * LDAP import - * - * @author Aladin Alaily - * @return View - */ - public function getLDAP() - { - // Get all the available groups - $groups = Sentry::getGroupProvider()->findAll(); - // Selected groups - $selectedGroups = Input::old('groups', array()); - // Get all the available permissions - $permissions = Config::get('permissions'); - $this->encodeAllPermissions($permissions); - // Selected permissions - $selectedPermissions = Input::old('permissions', array('superuser' => -1)); - $this->encodePermissions($selectedPermissions); - // Show the page - return View::make('backend/users/ldap', compact('groups', 'selectedGroups', 'permissions', 'selectedPermissions')); - } - - - /** + /** * Declare the rules for the form validation * * @var array */ protected $ldapValidationRules = array( - 'firstname' => 'required|alpha_space|min:2', - 'lastname' => 'required|alpha_space|min:2', - 'pycyin' => 'numeric', - 'username' => 'required|min:2|unique:users,username', - 'mail' => 'email|unique:users,email', + 'firstname' => 'required|alpha_space|min:2', + 'lastname' => 'required|alpha_space|min:2', + 'pycyin' => 'numeric', + 'username' => 'required|min:2|unique:users,username', + 'mail' => 'email|unique:users,email', ); - - /** - * LDAP form processing. - * - * @Auther Aldin Alaily - * @return Redirect - */ - public function postLDAP() - { - + + /** + * LDAP form processing. + * + * @Auther Aldin Alaily + * @return Redirect + */ + public function postLDAP() { + $url = Config::get('ldap.url'); $username = Config::get('ldap.username'); $password = Config::get('ldap.password'); @@ -1169,7 +1095,6 @@ public function postLDAP() if ($results[$i]["pyactive"][0] == "TRUE") { $item = array(); - $item["username"] = $results[$i]["pyusername"][0]; $item["pycyin"] = $results[$i]["pycyin"][0]; $item["cn"] = $results[$i]["cn"][0]; @@ -1177,14 +1102,16 @@ public function postLDAP() $item["firstname"] = $results[$i]["givenname"][0]; $item["mail"] = $results[$i]["mail"][0]; - $user = DB::table('users')->where('username', $item['username'])->first(); + $user = DB::table('users')->where('username', $item["username"])->first(); if ($user) { $item["note"] = "exists"; } else { + + $validator = Validator::make($item, $this->ldapValidationRules); if ($validator->fails()) { - $item["note"] = "Validotor failed: " . $validator->messages(); + $item["note"] = "Validator failed: " . $validator->messages(); } else { // Create the user if they don't exist. @@ -1213,14 +1140,15 @@ public function postLDAP() // Update the user $updateuser->save(); - $item["note"] = "created" . " " . $pass . ""; + $item["note"] = "created"; } // Validator didn't fail } array_push($summary, $item); } - if ($i <= 1) + /* Easy break in the loop */ + if ($i <= 15) break; }