2016-03-25 01:18:05 -07:00
< ? php
namespace App\Http\Controllers ;
use App\Helpers\Helper ;
2018-07-16 17:44:03 -07:00
use App\Http\Requests\AssetFileRequest ;
use App\Http\Requests\SaveUserRequest ;
2016-03-25 01:18:05 -07:00
use App\Models\Accessory ;
use App\Models\Actionlog ;
use App\Models\Asset ;
use App\Models\Company ;
2018-07-16 17:44:03 -07:00
use App\Models\Group ;
use App\Models\Ldap ;
2017-01-11 04:27:05 -08:00
use App\Models\License ;
2018-07-16 17:44:03 -07:00
use App\Models\LicenseSeat ;
use App\Models\Location ;
2016-03-25 01:18:05 -07:00
use App\Models\Setting ;
use App\Models\User ;
2018-07-16 17:44:03 -07:00
use App\Notifications\WelcomeNotification ;
use Artisan ;
2016-03-25 01:18:05 -07:00
use Auth ;
use Config ;
use Crypt ;
use DB ;
2018-07-16 17:44:03 -07:00
use Gate ;
2016-03-25 01:18:05 -07:00
use HTML ;
2018-07-16 17:44:03 -07:00
use Illuminate\Database\Eloquent\ModelNotFoundException ;
use Illuminate\Http\Request ;
2016-03-25 01:18:05 -07:00
use Input ;
use Lang ;
use League\Csv\Reader ;
use Mail ;
use Redirect ;
use Response ;
use Str ;
use Symfony\Component\HttpFoundation\JsonResponse ;
2018-07-16 17:44:03 -07:00
use Symfony\Component\HttpFoundation\StreamedResponse ;
2016-03-25 01:18:05 -07:00
use URL ;
use View ;
2016-06-02 00:41:10 -07:00
2016-03-25 01:18:05 -07:00
/**
2016-04-07 13:21:09 -07:00
* This controller handles all actions related to Users for
* the Snipe - IT Asset Management application .
*
* @ version v1 . 0
2016-03-25 01:18:05 -07:00
*/
2016-06-21 12:22:10 -07:00
2016-03-25 01:18:05 -07:00
class UsersController extends Controller
{
/**
* Returns a view that invokes the ajax tables which actually contains
* the content for the users listing , which is generated in getDatatable () .
*
* @ author [ A . Gianotto ] [ < snipe @ snipe . net > ]
* @ see UsersController :: getDatatable () method that generates the JSON response
* @ since [ v1 . 0 ]
2016-12-19 22:00:50 -08:00
* @ return \Illuminate\Contracts\View\View
*/
2016-12-15 20:52:39 -08:00
public function index ()
2016-03-25 01:18:05 -07:00
{
2016-12-19 11:04:28 -08:00
$this -> authorize ( 'index' , User :: class );
2017-06-09 16:44:03 -07:00
return view ( 'users/index' );
2016-03-25 01:18:05 -07:00
}
/**
* Returns a view that displays the user creation form .
*
* @ author [ A . Gianotto ] [ < snipe @ snipe . net > ]
* @ since [ v1 . 0 ]
2016-12-19 22:00:50 -08:00
* @ return \Illuminate\Contracts\View\View
*/
2016-12-15 20:52:39 -08:00
public function create ()
2016-03-25 01:18:05 -07:00
{
2016-12-19 11:04:28 -08:00
$this -> authorize ( 'create' , User :: class );
2016-05-12 15:26:48 -07:00
$groups = Group :: pluck ( 'name' , 'id' );
if ( Input :: old ( 'groups' )) {
$userGroups = Group :: whereIn ( 'id' , Input :: old ( 'groups' )) -> pluck ( 'name' , 'id' );
2016-04-28 21:59:43 -07:00
} else {
$userGroups = collect ();
}
2016-06-15 11:51:10 -07:00
2016-03-25 01:18:05 -07:00
$permissions = config ( 'permissions' );
2016-05-09 15:40:27 -07:00
$userPermissions = Helper :: selectedPermissionsArray ( $permissions , Input :: old ( 'permissions' , array ()));
2016-10-12 12:06:28 -07:00
$permissions = $this -> filterDisplayable ( $permissions );
2016-03-25 01:18:05 -07:00
2018-08-14 18:14:41 -07:00
$user = new User ;
$user -> activated = 1 ;
2017-06-09 16:44:03 -07:00
return view ( 'users/edit' , compact ( 'groups' , 'userGroups' , 'permissions' , 'userPermissions' ))
2018-08-14 18:14:41 -07:00
-> with ( 'user' , $user );
2016-03-25 01:18:05 -07:00
}
/**
* Validate and store the new user data , or return an error .
*
* @ author [ A . Gianotto ] [ < snipe @ snipe . net > ]
* @ since [ v1 . 0 ]
2016-12-19 22:00:50 -08:00
* @ return \Illuminate\Http\RedirectResponse
*/
2016-12-15 20:52:39 -08:00
public function store ( SaveUserRequest $request )
2016-03-25 01:18:05 -07:00
{
2016-12-19 11:04:28 -08:00
$this -> authorize ( 'create' , User :: class );
2016-03-25 19:26:22 -07:00
$user = new User ;
2016-06-27 19:47:21 -07:00
//Username, email, and password need to be handled specially because the need to respect config values on an edit.
2016-06-06 14:15:50 -07:00
$user -> email = $data [ 'email' ] = e ( $request -> input ( 'email' ));
$user -> username = $data [ 'username' ] = e ( $request -> input ( 'username' ));
2019-05-23 17:39:50 -07:00
if ( $request -> filled ( 'password' )) {
2016-06-06 14:15:50 -07:00
$user -> password = bcrypt ( $request -> input ( 'password' ));
$data [ 'password' ] = $request -> input ( 'password' );
}
2016-12-19 22:00:50 -08:00
$user -> first_name = $request -> input ( 'first_name' );
$user -> last_name = $request -> input ( 'last_name' );
$user -> locale = $request -> input ( 'locale' );
$user -> employee_num = $request -> input ( 'employee_num' );
2018-07-24 13:28:59 -07:00
$user -> activated = $request -> input ( 'activated' , 0 );
2016-12-19 22:00:50 -08:00
$user -> jobtitle = $request -> input ( 'jobtitle' );
$user -> phone = $request -> input ( 'phone' );
2016-12-26 15:17:46 -08:00
$user -> location_id = $request -> input ( 'location_id' , null );
2017-05-23 02:47:49 -07:00
$user -> department_id = $request -> input ( 'department_id' , null );
2016-12-26 15:17:46 -08:00
$user -> company_id = Company :: getIdForUser ( $request -> input ( 'company_id' , null ));
$user -> manager_id = $request -> input ( 'manager_id' , null );
2016-12-19 22:00:50 -08:00
$user -> notes = $request -> input ( 'notes' );
2017-10-30 18:57:00 -07:00
$user -> address = $request -> input ( 'address' , null );
$user -> city = $request -> input ( 'city' , null );
$user -> state = $request -> input ( 'state' , null );
$user -> country = $request -> input ( 'country' , null );
$user -> zip = $request -> input ( 'zip' , null );
2016-10-31 19:08:24 -07:00
// Strip out the superuser permission if the user isn't a superadmin
$permissions_array = $request -> input ( 'permission' );
if ( ! Auth :: user () -> isSuperUser ()) {
unset ( $permissions_array [ 'superuser' ]);
}
$user -> permissions = json_encode ( $permissions_array );
2016-06-15 11:51:10 -07:00
if ( $user -> save ()) {
2016-05-17 21:15:29 -07:00
2019-05-23 17:39:50 -07:00
if ( $request -> filled ( 'groups' )) {
2016-06-15 11:51:10 -07:00
$user -> groups () -> sync ( $request -> input ( 'groups' ));
} else {
$user -> groups () -> sync ( array ());
}
2016-07-28 05:49:41 -07:00
2019-05-23 17:39:50 -07:00
if (( $request -> input ( 'email_user' ) == 1 ) && ( $request -> filled ( 'email' ))) {
2016-03-25 01:18:05 -07:00
// Send the credentials through email
$data = array ();
2016-06-06 14:15:50 -07:00
$data [ 'email' ] = e ( $request -> input ( 'email' ));
$data [ 'username' ] = e ( $request -> input ( 'username' ));
$data [ 'first_name' ] = e ( $request -> input ( 'first_name' ));
2018-03-03 14:37:42 -08:00
$data [ 'last_name' ] = e ( $request -> input ( 'last_name' ));
2016-06-06 14:15:50 -07:00
$data [ 'password' ] = e ( $request -> input ( 'password' ));
2016-03-25 01:18:05 -07:00
2018-03-03 12:44:41 -08:00
$user -> notify ( new WelcomeNotification ( $data ));
/* Mail :: send ( 'emails.send-login' , $data , function ( $m ) use ( $user ) {
2016-03-25 01:18:05 -07:00
$m -> to ( $user -> email , $user -> first_name . ' ' . $user -> last_name );
2016-09-20 07:20:10 -07:00
$m -> replyTo ( config ( 'mail.reply_to.address' ), config ( 'mail.reply_to.name' ));
2016-09-26 14:13:07 -07:00
$m -> subject ( trans ( 'mail.welcome' , [ 'name' => $user -> first_name ]));
2018-03-03 12:44:41 -08:00
}); */
2016-03-25 01:18:05 -07:00
}
2016-12-19 10:42:33 -08:00
return redirect :: route ( 'users.index' ) -> with ( 'success' , trans ( 'admin/users/message.success.create' ));
2016-03-25 01:18:05 -07:00
}
2016-06-22 12:27:41 -07:00
return redirect () -> back () -> withInput () -> withErrors ( $user -> getErrors ());
2016-03-25 01:18:05 -07:00
}
2018-07-16 17:44:03 -07:00
private function filterDisplayable ( $permissions )
{
$output = null ;
foreach ( $permissions as $key => $permission ) {
$output [ $key ] = array_filter ( $permission , function ( $p ) {
return $p [ 'display' ] === true ;
});
2016-03-25 01:18:05 -07:00
}
2018-07-16 17:44:03 -07:00
return $output ;
2016-03-25 01:18:05 -07:00
}
/**
2016-12-19 22:00:50 -08:00
* Returns a view that displays the edit user form
*
* @ author [ A . Gianotto ] [ < snipe @ snipe . net > ]
* @ since [ v1 . 0 ]
* @ param $permissions
* @ return View
* @ internal param int $id
*/
2017-05-23 02:47:49 -07:00
public function edit ( $id )
2016-03-25 01:18:05 -07:00
{
2017-05-23 02:47:49 -07:00
2017-10-28 11:17:52 -07:00
if ( $user = User :: find ( $id )) {
2016-12-19 11:04:28 -08:00
$this -> authorize ( 'update' , $user );
2016-04-28 21:59:43 -07:00
$permissions = config ( 'permissions' );
2016-03-25 01:18:05 -07:00
2016-04-28 21:59:43 -07:00
$groups = Group :: pluck ( 'name' , 'id' );
2016-03-25 01:18:05 -07:00
2016-04-28 21:59:43 -07:00
$userGroups = $user -> groups () -> pluck ( 'name' , 'id' );
$user -> permissions = $user -> decodePermissions ();
$userPermissions = Helper :: selectedPermissionsArray ( $permissions , $user -> permissions );
2016-10-12 12:06:28 -07:00
$permissions = $this -> filterDisplayable ( $permissions );
2016-03-25 01:18:05 -07:00
2017-10-28 11:17:52 -07:00
return view ( 'users/edit' , compact ( 'user' , 'groups' , 'userGroups' , 'permissions' , 'userPermissions' )) -> with ( 'item' , $user );
2016-03-25 01:18:05 -07:00
}
2017-10-28 11:17:52 -07:00
$error = trans ( 'admin/users/message.user_not_found' , compact ( 'id' ));
return redirect () -> route ( 'users.index' ) -> with ( 'error' , $error );
2016-03-25 01:18:05 -07:00
}
/**
2016-12-19 22:00:50 -08:00
* Validate and save edited user data from edit form .
*
* @ author [ A . Gianotto ] [ < snipe @ snipe . net > ]
* @ since [ v1 . 0 ]
* @ param UpdateUserRequest $request
* @ param int $id
* @ return \Illuminate\Http\RedirectResponse
*/
2017-08-22 20:32:39 -07:00
public function update ( SaveUserRequest $request , $id = null )
2016-03-25 01:18:05 -07:00
{
// We need to reverse the UI specific logic for our
// permissions here before we update the user.
2016-06-06 14:15:50 -07:00
$permissions = $request -> input ( 'permissions' , array ());
2016-03-25 01:18:05 -07:00
app ( 'request' ) -> request -> set ( 'permissions' , $permissions );
// Only update the email address if locking is set to false
if ( config ( 'app.lock_passwords' )) {
2016-12-15 20:52:39 -08:00
return redirect () -> route ( 'users.index' ) -> with ( 'error' , 'Denied! You cannot update user information on the demo.' );
2016-03-25 01:18:05 -07:00
}
try {
2016-11-11 19:48:39 -08:00
2016-03-25 01:18:05 -07:00
$user = User :: find ( $id );
2017-11-03 12:48:00 -07:00
if ( $user -> id == $request -> input ( 'manager_id' )) {
return redirect () -> back () -> withInput () -> with ( 'error' , 'You cannot be your own manager.' );
}
2016-12-19 11:04:28 -08:00
$this -> authorize ( 'update' , $user );
2016-10-31 19:37:24 -07:00
// Figure out of this user was an admin before this edit
$orig_permissions_array = $user -> decodePermissions ();
2016-12-19 22:00:50 -08:00
$orig_superuser = '0' ;
2016-11-11 19:48:39 -08:00
if ( is_array ( $orig_permissions_array )) {
if ( array_key_exists ( 'superuser' , $orig_permissions_array )) {
$orig_superuser = $orig_permissions_array [ 'superuser' ];
}
2016-10-31 19:37:24 -07:00
}
2018-07-21 23:02:06 -07:00
} catch ( ModelNotFoundException $e ) {
return redirect () -> route ( 'users.index' )
2018-07-23 06:47:21 -07:00
-> with ( 'error' , trans ( 'admin/users/message.user_not_found' , compact ( 'id' )));
2016-03-25 01:18:05 -07:00
}
2016-10-31 18:57:35 -07:00
// Only save groups if the user is a super user
if ( Auth :: user () -> isSuperUser ()) {
2019-02-12 23:43:38 -08:00
$user -> groups () -> sync ( $request -> input ( 'groups' ));
2016-06-15 20:45:45 -07:00
}
2016-10-31 18:57:35 -07:00
2017-08-22 20:32:39 -07:00
2019-05-23 17:39:50 -07:00
if ( $request -> filled ( 'username' )) {
2017-08-22 20:32:39 -07:00
$user -> username = $request -> input ( 'username' );
2016-03-25 01:18:05 -07:00
}
2017-08-22 20:32:39 -07:00
$user -> email = $request -> input ( 'email' );
2016-03-25 01:18:05 -07:00
2016-06-27 19:47:21 -07:00
// Update the user
2016-12-19 22:00:50 -08:00
$user -> first_name = $request -> input ( 'first_name' );
$user -> last_name = $request -> input ( 'last_name' );
2016-12-23 17:52:00 -08:00
$user -> two_factor_optin = $request -> input ( 'two_factor_optin' ) ? : 0 ;
2016-12-19 22:00:50 -08:00
$user -> locale = $request -> input ( 'locale' );
$user -> employee_num = $request -> input ( 'employee_num' );
2018-07-18 08:27:26 -07:00
$user -> activated = $request -> input ( 'activated' , 0 );
2017-10-30 18:57:00 -07:00
$user -> jobtitle = $request -> input ( 'jobtitle' , null );
2016-12-19 22:00:50 -08:00
$user -> phone = $request -> input ( 'phone' );
2019-05-21 18:55:12 -07:00
$user -> website = $request -> input ( 'website' , null );
2016-12-26 15:17:46 -08:00
$user -> location_id = $request -> input ( 'location_id' , null );
$user -> company_id = Company :: getIdForUser ( $request -> input ( 'company_id' , null ));
$user -> manager_id = $request -> input ( 'manager_id' , null );
2016-12-19 22:00:50 -08:00
$user -> notes = $request -> input ( 'notes' );
2017-05-23 02:47:49 -07:00
$user -> department_id = $request -> input ( 'department_id' , null );
2017-10-30 18:57:00 -07:00
$user -> address = $request -> input ( 'address' , null );
$user -> city = $request -> input ( 'city' , null );
$user -> state = $request -> input ( 'state' , null );
$user -> country = $request -> input ( 'country' , null );
2018-07-16 23:48:46 -07:00
$user -> activated = $request -> input ( 'activated' , 0 );
2017-10-30 18:57:00 -07:00
$user -> zip = $request -> input ( 'zip' , null );
2016-06-27 19:47:21 -07:00
2017-10-30 19:33:52 -07:00
// Update the location of any assets checked out to this user
Asset :: where ( 'assigned_type' , User :: class )
-> where ( 'assigned_to' , $user -> id ) -> update ([ 'location_id' => $request -> input ( 'location_id' , null )]);
2017-08-22 20:32:39 -07:00
// Do we want to update the user password?
2019-05-23 17:39:50 -07:00
if ( $request -> filled ( 'password' )) {
2017-08-22 20:32:39 -07:00
$user -> password = bcrypt ( $request -> input ( 'password' ));
}
2016-06-27 19:47:21 -07:00
2016-10-31 19:07:55 -07:00
// Strip out the superuser permission if the user isn't a superadmin
$permissions_array = $request -> input ( 'permission' );
if ( ! Auth :: user () -> isSuperUser ()) {
unset ( $permissions_array [ 'superuser' ]);
2016-10-31 19:37:24 -07:00
$permissions_array [ 'superuser' ] = $orig_superuser ;
2016-06-27 19:47:21 -07:00
}
2016-10-31 19:07:55 -07:00
$user -> permissions = json_encode ( $permissions_array );
2016-06-27 19:47:21 -07:00
2016-12-19 22:00:50 -08:00
// Was the user updated?
2016-06-27 21:11:59 -07:00
if ( $user -> save ()) {
// Prepare the success message
$success = trans ( 'admin/users/message.success.update' );
// Redirect to the user page
2016-12-15 20:52:39 -08:00
return redirect () -> route ( 'users.index' ) -> with ( 'success' , $success );
2016-06-27 21:11:59 -07:00
}
2016-12-19 22:00:50 -08:00
return redirect () -> back () -> withInput () -> withErrors ( $user -> getErrors ());
2016-06-27 19:47:21 -07:00
}
2016-03-25 01:18:05 -07:00
/**
* Delete a user
*
* @ author [ A . Gianotto ] [ < snipe @ snipe . net > ]
* @ since [ v1 . 0 ]
* @ param int $id
2016-12-19 22:00:50 -08:00
* @ return \Illuminate\Http\RedirectResponse
*/
2016-12-15 20:52:39 -08:00
public function destroy ( $id = null )
2016-03-25 01:18:05 -07:00
{
try {
// Get user information
2018-07-16 17:44:03 -07:00
$user = User :: findOrFail ( $id );
2016-12-19 11:04:28 -08:00
// Authorize takes care of many of our logic checks now.
$this -> authorize ( 'delete' , User :: class );
2016-03-25 01:18:05 -07:00
// Check if we are not trying to delete ourselves
if ( $user -> id === Auth :: user () -> id ) {
// Redirect to the user management page
2019-07-17 17:51:13 -07:00
return redirect () -> route ( 'users.index' ) -> with ( 'error' , 'You cannot delete yourself.' );
2016-03-25 01:18:05 -07:00
}
2019-07-17 17:51:13 -07:00
if (( $user -> assets ) && ( $user -> assets -> count () > 0 )) {
2017-09-28 19:57:52 -07:00
// Redirect to the user management page
2019-07-17 17:51:13 -07:00
return redirect () -> route ( 'users.index' ) -> with ( 'error' , 'This user still has ' . $user -> assets -> count () . ' assets associated with them. Use the Checkin and Delete button on the user profile to check these items back in and delete this user.' );
2017-09-28 19:57:52 -07:00
}
2019-07-17 17:51:13 -07:00
if (( $user -> licenses ) && ( $user -> licenses -> count () > 0 )) {
2016-03-25 01:18:05 -07:00
// Redirect to the user management page
2019-07-17 17:51:13 -07:00
return redirect () -> route ( 'users.index' ) -> with ( 'error' , 'This user still has ' . $user -> licenses -> count () . ' license(s associated with them. Use the Checkin and Delete button on the user profile to check these items back in and delete this user.' );
2016-03-25 01:18:05 -07:00
}
2019-07-17 17:51:13 -07:00
if (( $user -> accessories ) && ( $user -> accessories -> count () > 0 )) {
2016-03-25 01:18:05 -07:00
// Redirect to the user management page
2019-07-17 17:51:13 -07:00
return redirect () -> route ( 'users.index' ) -> with ( 'error' , 'This user still has ' . $user -> accessories -> count () . ' accessories associated with them. Use the Checkin and Delete button on the user profile to check these items back in and delete this user.' );
2016-03-25 01:18:05 -07:00
}
2019-07-17 17:51:13 -07:00
if (( $user -> managedLocations ()) && ( $user -> managedLocations () -> count () > 0 )) {
2016-04-21 21:01:45 -07:00
// Redirect to the user management page
2017-05-22 17:27:00 -07:00
return redirect () -> route ( 'users.index' ) -> with ( 'error' , 'This user still has ' . $user -> managedLocations () -> count () . ' locations that they manage.' );
2016-04-21 21:01:45 -07:00
}
2016-03-25 01:18:05 -07:00
// Delete the user
$user -> delete ();
// Prepare the success message
2016-04-07 13:39:35 -07:00
$success = trans ( 'admin/users/message.success.delete' );
2016-03-25 01:18:05 -07:00
// Redirect to the user management page
2016-12-15 20:52:39 -08:00
return redirect () -> route ( 'users.index' ) -> with ( 'success' , $success );
2018-07-16 17:44:03 -07:00
} catch ( ModelNotFoundException $e ) {
2016-03-25 01:18:05 -07:00
// Prepare the error message
2016-04-07 13:39:35 -07:00
$error = trans ( 'admin/users/message.user_not_found' , compact ( 'id' ));
2016-03-25 01:18:05 -07:00
// Redirect to the user management page
2016-12-15 20:52:39 -08:00
return redirect () -> route ( 'users.index' ) -> with ( 'error' , $error );
2016-03-25 01:18:05 -07:00
}
}
/**
* Returns a view that confirms the user ' s a bulk delete will be applied to .
*
* @ author [ A . Gianotto ] [ < snipe @ snipe . net > ]
* @ since [ v1 . 7 ]
2016-12-19 22:00:50 -08:00
* @ return \Illuminate\Contracts\View\View
*/
2017-03-10 20:07:44 -08:00
public function postBulkEdit ( Request $request )
2016-03-25 01:18:05 -07:00
{
2016-12-19 11:04:28 -08:00
$this -> authorize ( 'update' , User :: class );
2017-03-10 20:07:44 -08:00
2019-05-23 17:39:50 -07:00
if (( $request -> filled ( 'ids' )) && ( count ( $request -> input ( 'ids' )) > 0 )) {
2016-03-25 01:18:05 -07:00
$statuslabel_list = Helper :: statusLabelList ();
2017-01-18 19:38:42 -08:00
$user_raw_array = array_keys ( Input :: get ( 'ids' ));
2016-06-22 12:27:41 -07:00
$users = User :: whereIn ( 'id' , $user_raw_array ) -> with ( 'groups' , 'assets' , 'licenses' , 'accessories' ) -> get ();
2018-04-29 06:10:49 -07:00
if ( $request -> input ( 'bulk_actions' ) == 'edit' ) {
2017-06-09 16:44:03 -07:00
return view ( 'users/bulk-edit' , compact ( 'users' ))
2017-03-10 20:07:44 -08:00
-> with ( 'groups' , Group :: pluck ( 'name' , 'id' ));
}
2017-06-09 16:44:03 -07:00
return view ( 'users/confirm-bulk-delete' , compact ( 'users' , 'statuslabel_list' ));
2016-03-25 01:18:05 -07:00
}
2018-04-29 06:10:49 -07:00
return redirect () -> back () -> with ( 'error' , 'No users selected' );
2016-03-25 01:18:05 -07:00
}
2017-03-10 20:07:44 -08:00
/**
* Save bulk - edited users
*
* @ author [ A . Gianotto ] [ < snipe @ snipe . net > ]
* @ since [ v1 . 0 ]
* @ return \Illuminate\Http\RedirectResponse
*/
public function postBulkEditSave ( Request $request )
{
$this -> authorize ( 'update' , User :: class );
2019-05-23 17:39:50 -07:00
if (( $request -> filled ( 'ids' )) && ( count ( $request -> input ( 'ids' )) > 0 )) {
2018-04-29 06:10:49 -07:00
$user_raw_array = $request -> input ( 'ids' );
2017-03-10 20:07:44 -08:00
$update_array = array ();
2017-05-23 02:47:49 -07:00
$manager_conflict = false ;
2018-04-29 06:10:49 -07:00
$users = User :: whereIn ( 'id' , $user_raw_array ) -> where ( 'id' , '!=' , Auth :: user () -> id ) -> get ();
2017-03-10 20:07:44 -08:00
2019-05-23 17:39:50 -07:00
if ( $request -> filled ( 'location_id' )) {
2017-03-10 20:07:44 -08:00
$update_array [ 'location_id' ] = $request -> input ( 'location_id' );
}
2019-05-23 17:39:50 -07:00
if ( $request -> filled ( 'department_id' )) {
2017-05-23 02:47:49 -07:00
$update_array [ 'department_id' ] = $request -> input ( 'department_id' );
}
2019-05-23 17:39:50 -07:00
if ( $request -> filled ( 'company_id' )) {
2017-03-10 20:07:44 -08:00
$update_array [ 'company_id' ] = $request -> input ( 'company_id' );
}
2019-05-23 17:39:50 -07:00
if ( $request -> filled ( 'locale' )) {
2018-01-10 05:33:26 -08:00
$update_array [ 'locale' ] = $request -> input ( 'locale' );
}
2019-05-23 17:39:50 -07:00
if ( $request -> filled ( 'manager_id' )) {
2017-03-10 20:07:44 -08:00
// Do not allow a manager update if the selected manager is one of the users being
// edited.
if ( ! array_key_exists ( $request -> input ( 'manager_id' ), $user_raw_array )) {
$update_array [ 'manager_id' ] = $request -> input ( 'manager_id' );
} else {
$manager_conflict = true ;
}
}
2019-05-23 17:39:50 -07:00
if ( $request -> filled ( 'activated' )) {
2017-03-10 20:07:44 -08:00
$update_array [ 'activated' ] = $request -> input ( 'activated' );
}
2018-04-29 06:10:49 -07:00
// Save the updated info
2017-03-10 20:07:44 -08:00
if ( count ( $update_array ) > 0 ) {
2018-04-29 06:10:49 -07:00
User :: whereIn ( 'id' , $user_raw_array ) -> where ( 'id' , '!=' , Auth :: user () -> id ) -> update ( $update_array );
2017-03-10 20:07:44 -08:00
}
// Only sync groups if groups were selected
2019-05-23 17:39:50 -07:00
if ( $request -> filled ( 'groups' )) {
2017-03-10 20:07:44 -08:00
foreach ( $users as $user ) {
$user -> groups () -> sync ( $request -> input ( 'groups' ));
}
}
2018-04-29 06:10:49 -07:00
if ( $manager_conflict ) {
return redirect () -> route ( 'users.index' )
-> with ( 'warning' , trans ( 'admin/users/message.bulk_manager_warn' ));
}
2017-03-10 20:07:44 -08:00
return redirect () -> route ( 'users.index' )
2018-04-29 06:10:49 -07:00
-> with ( 'success' , trans ( 'admin/users/message.success.update_bulk' ));
2017-03-10 20:07:44 -08:00
}
2018-04-29 06:10:49 -07:00
return redirect () -> back () -> with ( 'error' , 'No users selected' );
2017-03-10 20:07:44 -08:00
}
2016-03-25 01:18:05 -07:00
/**
* Soft - delete bulk users
*
* @ author [ A . Gianotto ] [ < snipe @ snipe . net > ]
* @ since [ v1 . 0 ]
2016-12-19 22:00:50 -08:00
* @ return \Illuminate\Http\RedirectResponse
*/
2018-05-16 19:35:14 -07:00
public function postBulkSave ( Request $request )
2016-03-25 01:18:05 -07:00
{
2016-12-19 11:04:28 -08:00
$this -> authorize ( 'update' , User :: class );
2018-05-16 19:38:02 -07:00
2019-05-23 17:39:50 -07:00
if (( ! $request -> filled ( 'ids' )) || ( count ( $request -> input ( 'ids' )) == 0 )) {
2016-04-28 21:06:41 -07:00
return redirect () -> back () -> with ( 'error' , 'No users selected' );
2019-05-23 17:39:50 -07:00
} elseif (( ! $request -> filled ( 'status_id' )) || ( $request -> input ( 'status_id' ) == '' )) {
2016-12-15 20:52:39 -08:00
return redirect () -> route ( 'users.index' ) -> with ( 'error' , 'No status selected' );
2016-03-25 01:18:05 -07:00
} else {
2017-01-18 19:38:42 -08:00
$user_raw_array = Input :: get ( 'ids' );
2016-03-25 01:18:05 -07:00
$asset_array = array ();
if (( $key = array_search ( Auth :: user () -> id , $user_raw_array )) !== false ) {
unset ( $user_raw_array [ $key ]);
}
2018-07-16 17:44:03 -07:00
2016-03-25 01:18:05 -07:00
if ( ! config ( 'app.lock_passwords' )) {
2016-05-12 21:01:31 -07:00
$users = User :: whereIn ( 'id' , $user_raw_array ) -> get ();
2016-03-25 01:18:05 -07:00
$assets = Asset :: whereIn ( 'assigned_to' , $user_raw_array ) -> get ();
$accessories = DB :: table ( 'accessories_users' ) -> whereIn ( 'assigned_to' , $user_raw_array ) -> get ();
2016-05-12 21:01:31 -07:00
$licenses = DB :: table ( 'license_seats' ) -> whereIn ( 'assigned_to' , $user_raw_array ) -> get ();
$license_array = array ();
$accessory_array = array ();
2016-03-25 01:18:05 -07:00
foreach ( $assets as $asset ) {
$asset_array [] = $asset -> id ;
// Update the asset log
2016-12-19 22:00:50 -08:00
$logAction = new Actionlog ();
$logAction -> item_id = $asset -> id ;
$logAction -> item_type = Asset :: class ;
$logAction -> target_id = $asset -> assigned_to ;
$logAction -> target_type = User :: class ;
$logAction -> user_id = Auth :: user () -> id ;
$logAction -> note = 'Bulk checkin asset and delete user' ;
$logAction -> logaction ( 'checkin from' );
Asset :: whereIn ( 'id' , $asset_array ) -> update ([
2016-03-25 01:18:05 -07:00
'status_id' => e ( Input :: get ( 'status_id' )),
'assigned_to' => null ,
2016-12-19 22:00:50 -08:00
]);
2016-03-25 01:18:05 -07:00
}
foreach ( $accessories as $accessory ) {
2016-05-12 21:01:31 -07:00
$accessory_array [] = $accessory -> accessory_id ;
2016-03-25 01:18:05 -07:00
// Update the asset log
2016-12-19 22:00:50 -08:00
$logAction = new Actionlog ();
$logAction -> item_id = $accessory -> id ;
$logAction -> item_type = Accessory :: class ;
$logAction -> target_id = $accessory -> assigned_to ;
$logAction -> target_type = User :: class ;
$logAction -> user_id = Auth :: user () -> id ;
$logAction -> note = 'Bulk checkin accessory and delete user' ;
$logAction -> logaction ( 'checkin from' );
2016-05-12 21:01:31 -07:00
}
foreach ( $licenses as $license ) {
$license_array [] = $license -> id ;
// Update the asset log
2016-12-19 22:00:50 -08:00
$logAction = new Actionlog ();
$logAction -> item_id = $license -> id ;
$logAction -> item_type = License :: class ;
$logAction -> target_id = $license -> assigned_to ;
$logAction -> target_type = User :: class ;
$logAction -> user_id = Auth :: user () -> id ;
$logAction -> note = 'Bulk checkin license and delete user' ;
$logAction -> logaction ( 'checkin from' );
2016-03-25 01:18:05 -07:00
}
2016-06-15 11:51:10 -07:00
2016-06-22 12:27:41 -07:00
LicenseSeat :: whereIn ( 'id' , $license_array ) -> update ([ 'assigned_to' => null ]);
2016-05-12 21:01:31 -07:00
foreach ( $users as $user ) {
$user -> accessories () -> sync ( array ());
$user -> delete ();
}
2016-12-15 20:52:39 -08:00
return redirect () -> route ( 'users.index' ) -> with ( 'success' , 'Your selected users have been deleted and their assets have been updated.' );
2016-03-25 01:18:05 -07:00
}
2016-12-19 22:00:50 -08:00
return redirect () -> route ( 'users.index' ) -> with ( 'error' , 'Bulk delete is not enabled in this installation' );
2016-03-25 01:18:05 -07:00
}
}
/**
* Restore a deleted user
*
* @ author [ A . Gianotto ] [ < snipe @ snipe . net > ]
* @ since [ v1 . 0 ]
* @ param int $id
2016-12-19 22:00:50 -08:00
* @ return \Illuminate\Http\RedirectResponse
*/
2016-03-25 01:18:05 -07:00
public function getRestore ( $id = null )
{
2016-12-19 11:04:28 -08:00
$this -> authorize ( 'edit' , User :: class );
2016-03-25 01:18:05 -07:00
// Get user information
2016-06-22 12:27:41 -07:00
if ( ! $user = User :: onlyTrashed () -> find ( $id )) {
2016-12-15 20:52:39 -08:00
return redirect () -> route ( 'users.index' ) -> with ( 'error' , trans ( 'admin/users/messages.user_not_found' ));
2016-06-22 12:27:41 -07:00
}
2016-04-21 21:01:45 -07:00
2016-12-19 11:04:28 -08:00
// Restore the user
if ( User :: withTrashed () -> where ( 'id' , $id ) -> restore ()) {
return redirect () -> route ( 'users.index' ) -> with ( 'success' , trans ( 'admin/users/message.success.restored' ));
2016-06-22 12:27:41 -07:00
}
2016-12-19 11:04:28 -08:00
return redirect () -> route ( 'users.index' ) -> with ( 'error' , 'User could not be restored.' );
2016-03-25 01:18:05 -07:00
}
/**
* Return a view with user detail
*
* @ author [ A . Gianotto ] [ < snipe @ snipe . net > ]
* @ since [ v1 . 0 ]
* @ param int $userId
2016-12-19 22:00:50 -08:00
* @ return \Illuminate\Contracts\View\View
*/
2016-12-15 20:52:39 -08:00
public function show ( $userId = null )
2016-03-25 01:18:05 -07:00
{
2020-04-07 17:26:56 -07:00
if ( ! $user = User :: with ( 'assets' , 'assets.model' , 'consumables' , 'accessories' , 'licenses' , 'userloc' )
-> withTrashed ()
-> find ( $userId ))
{
return redirect () -> route ( 'users.index' ) -> with ( 'error' , trans ( 'admin/users/message.user_not_found' , [ 'id' => $userId ]));
2016-12-19 11:04:28 -08:00
}
2016-03-25 01:18:05 -07:00
2016-09-06 19:39:42 -07:00
$userlog = $user -> userlog -> load ( 'item' );
2016-03-25 01:18:05 -07:00
if ( isset ( $user -> id )) {
2016-12-19 11:04:28 -08:00
$this -> authorize ( 'view' , $user );
2017-06-09 16:44:03 -07:00
return view ( 'users/view' , compact ( 'user' , 'userlog' ));
2016-03-25 01:18:05 -07:00
}
}
/**
* Unsuspend a user .
*
* @ author [ A . Gianotto ] [ < snipe @ snipe . net > ]
* @ since [ v1 . 0 ]
* @ param int $id
* @ return Redirect
*/
public function getUnsuspend ( $id = null )
{
try {
// Get user information
2016-12-19 11:04:28 -08:00
$user = User :: find ( $id );
$this -> authorize ( 'edit' , $user );
2016-03-25 01:18:05 -07:00
// Check if we are not trying to unsuspend ourselves
if ( $user -> id === Auth :: user () -> id ) {
// Prepare the error message
2016-04-07 13:39:35 -07:00
$error = trans ( 'admin/users/message.error.unsuspend' );
2016-03-25 01:18:05 -07:00
// Redirect to the user management page
2016-12-15 20:52:39 -08:00
return redirect () -> route ( 'users.index' ) -> with ( 'error' , $error );
2016-03-25 01:18:05 -07:00
}
// Do we have permission to unsuspend this user?
2016-07-10 20:55:44 -07:00
if ( $user -> isSuperUser () && ! Auth :: user () -> isSuperUser ()) {
2016-03-25 01:18:05 -07:00
// Redirect to the user management page
2016-12-15 20:52:39 -08:00
return redirect () -> route ( 'users.index' ) -> with ( 'error' , 'Insufficient permissions!' );
2016-03-25 01:18:05 -07:00
}
// Prepare the success message
2016-04-07 13:39:35 -07:00
$success = trans ( 'admin/users/message.success.unsuspend' );
2016-03-25 01:18:05 -07:00
// Redirect to the user management page
2016-12-15 20:52:39 -08:00
return redirect () -> route ( 'users.index' ) -> with ( 'success' , $success );
2016-03-25 01:18:05 -07:00
} catch ( UserNotFoundException $e ) {
// Prepare the error message
2016-04-07 13:39:35 -07:00
$error = trans ( 'admin/users/message.user_not_found' , compact ( 'id' ));
2016-03-25 01:18:05 -07:00
// Redirect to the user management page
2016-12-15 20:52:39 -08:00
return redirect () -> route ( 'users.index' ) -> with ( 'error' , $error );
2016-03-25 01:18:05 -07:00
}
}
/**
* Return a view containing a pre - populated new user form ,
* populated with some fields from an existing user .
*
* @ author [ A . Gianotto ] [ < snipe @ snipe . net > ]
* @ since [ v1 . 0 ]
* @ param int $id
2016-12-19 22:00:50 -08:00
* @ return \Illuminate\Contracts\View\View
*/
2016-03-25 01:18:05 -07:00
public function getClone ( $id = null )
{
2016-12-19 11:04:28 -08:00
$this -> authorize ( 'create' , User :: class );
2016-03-25 01:18:05 -07:00
// We need to reverse the UI specific logic for our
// permissions here before we update the user.
$permissions = Input :: get ( 'permissions' , array ());
//$this->decodePermissions($permissions);
app ( 'request' ) -> request -> set ( 'permissions' , $permissions );
try {
// Get the user information
2016-04-21 20:52:32 -07:00
$user_to_clone = User :: withTrashed () -> find ( $id );
2016-03-25 01:18:05 -07:00
$user = clone $user_to_clone ;
$user -> first_name = '' ;
$user -> last_name = '' ;
$user -> email = substr ( $user -> email , ( $pos = strpos ( $user -> email , '@' )) !== false ? $pos : 0 );
2017-10-28 11:17:52 -07:00
2016-03-25 01:18:05 -07:00
$user -> id = null ;
// Get this user groups
2017-07-07 18:45:49 -07:00
$userGroups = $user_to_clone -> groups () -> pluck ( 'name' , 'id' );
2016-03-25 01:18:05 -07:00
// Get all the available permissions
$permissions = config ( 'permissions' );
2016-07-10 18:43:10 -07:00
$clonedPermissions = $user_to_clone -> decodePermissions ();
$userPermissions = Helper :: selectedPermissionsArray ( $permissions , $clonedPermissions );
2016-03-25 01:18:05 -07:00
// Show the page
2017-06-09 16:44:03 -07:00
return view ( 'users/edit' , compact ( 'permissions' , 'userPermissions' ))
2016-03-25 01:18:05 -07:00
-> with ( 'user' , $user )
2016-12-19 22:00:50 -08:00
-> with ( 'groups' , Group :: pluck ( 'name' , 'id' ))
2016-06-22 12:27:41 -07:00
-> with ( 'userGroups' , $userGroups )
2016-03-25 01:18:05 -07:00
-> with ( 'clone_user' , $user_to_clone );
} catch ( UserNotFoundException $e ) {
2020-04-07 17:26:56 -07:00
return redirect () -> route ( 'users.index' ) -> with ( 'error' , trans ( 'admin/users/message.user_not_found' ));
2016-03-25 01:18:05 -07:00
}
}
/**
2016-12-19 22:00:50 -08:00
* Return JSON response with a list of user details for the getIndex () view .
*
* @ author [ A . Gianotto ] [ < snipe @ snipe . net > ]
* @ since [ v1 . 6 ]
* @ param AssetFileRequest $request
* @ param int $userId
* @ return string JSON
*/
2016-03-25 01:18:05 -07:00
public function postUpload ( AssetFileRequest $request , $userId = null )
{
$user = User :: find ( $userId );
2016-03-25 15:50:08 -07:00
$destinationPath = config ( 'app.private_uploads' ) . '/users' ;
2016-03-25 01:18:05 -07:00
if ( isset ( $user -> id )) {
2016-12-19 11:04:28 -08:00
$this -> authorize ( 'update' , $user );
2016-03-25 01:18:05 -07:00
2020-03-31 22:50:07 -07:00
if ( ! $request -> has ( 'file' )) {
\Log :: debug ( 'No file selected: ' );
\Log :: debug ( print_r ( $request , true ));
return redirect () -> back () -> with ( 'error' , 'No file submitted.' );
2016-03-25 01:18:05 -07:00
2020-03-31 22:50:07 -07:00
} else {
foreach ( $request -> file ( 'file' ) as $file ) {
$extension = $file -> getClientOriginalExtension ();
$filename = 'user-' . $user -> id . '-' . str_random ( 8 );
$filename .= '-' . str_slug ( $file -> getClientOriginalName ()) . '.' . $extension ;
$upload_success = $file -> move ( $destinationPath , $filename );
//Log the uploaded file to the log
$logAction = new Actionlog ();
$logAction -> item_id = $user -> id ;
$logAction -> item_type = User :: class ;
2020-04-01 02:22:16 -07:00
$logAction -> target_type = User :: class ;
$logAction -> target_id = $user -> id ;
2020-03-31 22:50:07 -07:00
$logAction -> user_id = Auth :: user () -> id ;
$logAction -> note = $request -> input ( 'notes' );
$logAction -> created_at = date ( " Y-m-d H:i:s " );
$logAction -> filename = $filename ;
$logAction -> action_type = 'uploaded' ;
$logAction -> save ();
}
return redirect () -> back () -> with ( 'success' , 'File uploaded' );
2016-03-25 01:18:05 -07:00
}
}
2020-03-31 22:50:07 -07:00
return redirect () -> route ( 'users.index' ) -> with ( 'error' , 'Error uploading files' );
2016-03-25 01:18:05 -07:00
}
/**
* Delete file
*
* @ author [ A . Gianotto ] [ < snipe @ snipe . net > ]
* @ since [ v1 . 6 ]
* @ param int $userId
* @ param int $fileId
2016-12-19 22:00:50 -08:00
* @ return \Illuminate\Http\RedirectResponse
*/
2016-03-25 01:18:05 -07:00
public function getDeleteFile ( $userId = null , $fileId = null )
{
$user = User :: find ( $userId );
2016-03-25 15:50:08 -07:00
$destinationPath = config ( 'app.private_uploads' ) . '/users' ;
2016-03-25 01:18:05 -07:00
if ( isset ( $user -> id )) {
2016-12-19 11:04:28 -08:00
$this -> authorize ( 'update' , $user );
$log = Actionlog :: find ( $fileId );
$full_filename = $destinationPath . '/' . $log -> filename ;
if ( file_exists ( $full_filename )) {
unlink ( $destinationPath . '/' . $log -> filename );
2016-03-25 01:18:05 -07:00
}
2016-12-19 11:04:28 -08:00
$log -> delete ();
return redirect () -> back () -> with ( 'success' , trans ( 'admin/users/message.deletefile.success' ));
2016-03-25 01:18:05 -07:00
}
2020-04-07 17:26:56 -07:00
return redirect () -> route ( 'users.index' ) -> with ( 'error' , trans ( 'admin/users/message.does_not_exist' ));
2016-12-19 11:04:28 -08:00
2016-03-25 01:18:05 -07:00
}
/**
* Display / download the uploaded file
*
* @ author [ A . Gianotto ] [ < snipe @ snipe . net > ]
* @ since [ v1 . 6 ]
* @ param int $userId
* @ param int $fileId
* @ return mixed
*/
public function displayFile ( $userId = null , $fileId = null )
{
$user = User :: find ( $userId );
// the license is valid
if ( isset ( $user -> id )) {
2016-12-19 11:04:28 -08:00
$this -> authorize ( 'view' , $user );
2016-03-25 01:18:05 -07:00
2016-12-19 11:04:28 -08:00
$log = Actionlog :: find ( $fileId );
$file = $log -> get_src ( 'users' );
return Response :: download ( $file );
2016-03-25 01:18:05 -07:00
}
2016-12-19 11:04:28 -08:00
// Prepare the error message
$error = trans ( 'admin/users/message.does_not_exist' , compact ( 'id' ));
// Redirect to the licence management page
return redirect () -> route ( 'users.index' ) -> with ( 'error' , $error );
2016-03-25 01:18:05 -07:00
}
/**
* Return view for LDAP import
*
* @ author Aladin Alaily
* @ since [ v1 . 8 ]
2016-12-19 22:00:50 -08:00
* @ return \Illuminate\Contracts\View\View
*/
2016-07-13 07:24:54 -07:00
public function getLDAP ()
2016-03-25 01:18:05 -07:00
{
2016-12-19 11:04:28 -08:00
$this -> authorize ( 'update' , User :: class );
2016-07-13 05:50:24 -07:00
try {
$ldapconn = Ldap :: connectToLdap ();
} catch ( \Exception $e ) {
2016-12-19 11:04:28 -08:00
return redirect () -> route ( 'users.index' ) -> with ( 'error' , $e -> getMessage ());
2016-07-13 05:50:24 -07:00
}
try {
Ldap :: bindAdminToLdap ( $ldapconn );
} catch ( \Exception $e ) {
2016-12-19 11:04:28 -08:00
return redirect () -> route ( 'users.index' ) -> with ( 'error' , $e -> getMessage ());
2016-07-13 05:50:24 -07:00
}
2018-07-16 14:22:25 -07:00
return view ( 'users/ldap' );
2016-03-25 01:18:05 -07:00
}
/**
* Declare the rules for the ldap fields validation .
*
* @ author Aladin Alaily
* @ since [ v1 . 8 ]
* @ var array
* @ deprecated 3.0
* @ todo remove this method in favor of other validation
* @ var array
*/
protected $ldapValidationRules = array (
'firstname' => 'required|string|min:2' ,
'employee_number' => 'string' ,
'username' => 'required|min:2|unique:users,username' ,
'email' => 'email|unique:users,email' ,
);
/**
* LDAP form processing .
*
* @ author Aladin Alaily
* @ since [ v1 . 8 ]
2016-12-19 22:00:50 -08:00
* @ return \Illuminate\Http\RedirectResponse
*/
2016-08-02 03:45:03 -07:00
public function postLDAP ( Request $request )
2016-03-25 01:18:05 -07:00
{
2017-10-06 16:15:14 -07:00
// Call Artisan LDAP import command.
$location_id = $request -> input ( 'location_id' );
Artisan :: call ( 'snipeit:ldap-sync' , [ '--location_id' => $location_id , '--json_summary' => true ]);
2016-03-25 01:18:05 -07:00
2017-10-06 16:15:14 -07:00
// Collect and parse JSON summary.
$ldap_results_json = Artisan :: output ();
$ldap_results = json_decode ( $ldap_results_json , true );
2017-01-11 23:37:14 -08:00
2017-10-06 16:15:14 -07:00
// Direct user to appropriate status page.
if ( $ldap_results [ 'error' ]) {
return redirect () -> back () -> withInput () -> with ( 'error' , $ldap_results [ 'error_message' ]);
} else {
return redirect () -> route ( 'ldap/user' ) -> with ( 'success' , " LDAP Import successful. " ) -> with ( 'summary' , $ldap_results [ 'summary' ]);
2016-03-25 01:18:05 -07:00
}
}
2017-08-26 15:22:04 -07:00
2016-10-25 02:41:34 -07:00
/**
* Exports users to CSV
*
* @ author [ A . Gianotto ] [ < snipe @ snipe . net > ]
* @ since [ v3 . 5 ]
2016-12-19 22:00:50 -08:00
* @ return StreamedResponse
2016-10-25 02:41:34 -07:00
*/
public function getExportUserCsv ()
{
2016-12-19 11:04:28 -08:00
$this -> authorize ( 'view' , User :: class );
2016-10-25 02:41:34 -07:00
\Debugbar :: disable ();
2016-12-29 14:02:18 -08:00
$response = new StreamedResponse ( function () {
2016-10-25 02:41:34 -07:00
// Open output stream
$handle = fopen ( 'php://output' , 'w' );
2018-02-01 13:54:49 -08:00
User :: with ( 'assets' , 'accessories' , 'consumables' , 'department' , 'licenses' , 'manager' , 'groups' , 'userloc' , 'company' , 'throttle' ) -> orderBy ( 'created_at' , 'DESC' ) -> chunk ( 500 , function ( $users ) use ( $handle ) {
2016-10-25 02:41:34 -07:00
$headers = [
// strtolower to prevent Excel from trying to open it as a SYLK file
strtolower ( trans ( 'general.id' )),
trans ( 'admin/companies/table.title' ),
2016-10-25 02:50:23 -07:00
trans ( 'admin/users/table.title' ),
2016-10-25 02:41:34 -07:00
trans ( 'admin/users/table.employee_num' ),
trans ( 'admin/users/table.name' ),
2016-10-25 02:50:23 -07:00
trans ( 'admin/users/table.username' ),
2016-10-25 02:41:34 -07:00
trans ( 'admin/users/table.email' ),
trans ( 'admin/users/table.manager' ),
trans ( 'admin/users/table.location' ),
2018-02-01 13:54:49 -08:00
trans ( 'general.department' ),
2016-10-25 02:41:34 -07:00
trans ( 'general.assets' ),
trans ( 'general.licenses' ),
trans ( 'general.accessories' ),
trans ( 'general.consumables' ),
trans ( 'admin/users/table.groups' ),
trans ( 'general.notes' ),
trans ( 'admin/users/table.activated' ),
trans ( 'general.created_at' )
];
2016-12-15 20:52:39 -08:00
2016-10-25 02:41:34 -07:00
fputcsv ( $handle , $headers );
foreach ( $users as $user ) {
$user_groups = '' ;
foreach ( $user -> groups as $user_group ) {
$user_groups .= $user_group -> name . ', ' ;
}
// Add a new row with data
$values = [
$user -> id ,
( $user -> company ) ? $user -> company -> name : '' ,
2016-10-25 02:50:23 -07:00
$user -> jobtitle ,
2016-10-25 02:41:34 -07:00
$user -> employee_num ,
2016-12-23 17:52:00 -08:00
$user -> present () -> fullName (),
2016-10-25 02:50:23 -07:00
$user -> username ,
2016-10-25 02:41:34 -07:00
$user -> email ,
2016-12-23 17:52:00 -08:00
( $user -> manager ) ? $user -> manager -> present () -> fullName () : '' ,
2016-12-22 15:53:34 -08:00
( $user -> userloc ) ? $user -> userloc -> name : '' ,
2018-02-01 13:54:49 -08:00
( $user -> department ) ? $user -> department -> name : '' ,
2016-10-25 02:41:34 -07:00
$user -> assets -> count (),
$user -> licenses -> count (),
$user -> accessories -> count (),
$user -> consumables -> count (),
$user_groups ,
$user -> notes ,
( $user -> activated == '1' ) ? trans ( 'general.yes' ) : trans ( 'general.no' ),
$user -> created_at ,
];
fputcsv ( $handle , $values );
}
});
// Close the output stream
fclose ( $handle );
}, 200 , [
'Content-Type' => 'text/csv' ,
'Content-Disposition' => 'attachment; filename="users-' . date ( 'Y-m-d-his' ) . '.csv"' ,
]);
return $response ;
}
2016-10-29 05:50:55 -07:00
2017-12-05 01:55:24 -08:00
/**
* LDAP form processing .
*
* @ author Aladin Alaily
* @ since [ v1 . 8 ]
* @ return \Illuminate\Http\RedirectResponse
*/
2017-12-06 11:17:42 -08:00
public function printInventory ( $id )
2017-12-05 01:55:24 -08:00
{
2018-10-19 16:43:28 -07:00
$this -> authorize ( 'view' , User :: class );
2017-12-06 11:17:42 -08:00
$show_user = User :: where ( 'id' , $id ) -> withTrashed () -> first ();
2017-12-05 12:34:16 -08:00
$assets = Asset :: where ( 'assigned_to' , $id ) -> where ( 'assigned_type' , User :: class ) -> with ( 'model' , 'model.category' ) -> get ();
2017-12-06 11:17:42 -08:00
$licenses = $show_user -> licenses () -> get ();
$accessories = $show_user -> accessories () -> get ();
$consumables = $show_user -> consumables () -> get ();
return view ( 'users/print' ) -> with ( 'assets' , $assets ) -> with ( 'licenses' , $licenses ) -> with ( 'accessories' , $accessories ) -> with ( 'consumables' , $consumables ) -> with ( 'show_user' , $show_user );
2017-12-05 01:55:24 -08:00
}
2016-03-25 01:18:05 -07:00
}