From 35155dfc4c80e7064c16381db491eeeccf3b05a2 Mon Sep 17 00:00:00 2001 From: snipe Date: Fri, 29 Nov 2013 16:57:24 -0500 Subject: [PATCH] Prevent user from being deleted if they still have assets assigned to them --- app/controllers/admin/UsersController.php | 17 +++++++++++++++-- app/models/Asset.php | 1 - 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/app/controllers/admin/UsersController.php b/app/controllers/admin/UsersController.php index 90023c4a91..2c9a594a3c 100755 --- a/app/controllers/admin/UsersController.php +++ b/app/controllers/admin/UsersController.php @@ -353,6 +353,7 @@ class UsersController extends AdminController { return Redirect::route('users')->with('error', $error); } + // Do we have permission to delete this user? if ($user->isSuperUser() and ! Sentry::getUser()->isSuperUser()) { @@ -360,14 +361,26 @@ class UsersController extends AdminController { return Redirect::route('users')->with('error', 'Insufficient permissions!'); } + if (count($user->assets) > 0) { + //print_r ($user->assets()); + // Redirect to the user management page + return Redirect::route('users')->with('error', 'This user still has '.count($user->assets).' assets associated with them.'); + } + + if (count($user->licenses) > 0) { + //print_r ($user->licenses()); + // Redirect to the user management page + return Redirect::route('users')->with('error', 'This user still has '.count($user->licenses).' licenses associated with them.'); + } + // Delete the user - $user->delete(); + //$user->delete(); // Prepare the success message $success = Lang::get('admin/users/message.success.delete'); // Redirect to the user management page - return Redirect::route('users')->with('success', $success); + //return Redirect::route('users')->with('success', $success); } catch (UserNotFoundException $e) { diff --git a/app/models/Asset.php b/app/models/Asset.php index 9ffe50744b..bd4fc09c64 100644 --- a/app/models/Asset.php +++ b/app/models/Asset.php @@ -83,7 +83,6 @@ class Asset extends Elegant { public static function assetcount() { return DB::table('assets') - ->where('physical', '=', '1') ->whereNull('deleted_at','and') ->count();