App\Http\Controllers\ViewAssetsController Class Reference

This controller handles all actions related to the ability for users to view their own assets in the Snipe-IT Asset Management application. More...

Inheritance diagram for App\Http\Controllers\ViewAssetsController:
App\Http\Controllers\Controller

Public Member Functions

 getIndex ()
 Redirect to the profile page. More...
 
 getRequestableIndex ()
 
 getRequestAsset ($assetId=null)
 
 getAcceptAsset ($logID=null)
 
 postAcceptAsset ($logID=null)
 

Detailed Description

This controller handles all actions related to the ability for users to view their own assets in the Snipe-IT Asset Management application.

Version
v1.0

Definition at line 29 of file ViewAssetsController.php.

Member Function Documentation

App\Http\Controllers\ViewAssetsController::getAcceptAsset (   $logID = null)

Definition at line 142 of file ViewAssetsController.php.

143  {
144 
145  if (is_null($findlog = Actionlog::find($logID))) {
146  // Redirect to the asset management page
147  return Redirect::to('account')->with('error', trans('admin/hardware/message.does_not_exist'));
148  }
149 
150  $user = Auth::user();
151 
152  if ($user->id != $findlog->checkedout_to) {
153  return Redirect::to('account/view-assets')->with('error', trans('admin/users/message.error.incorrect_user_accepted'));
154  }
155 
156  // Asset
157  if (($findlog->asset_id!='') && ($findlog->asset_type=='hardware')) {
158  $item = Asset::find($findlog->asset_id);
159 
160  // software
161  } elseif (($findlog->asset_id!='') && ($findlog->asset_type=='software')) {
162  $item = License::find($findlog->asset_id);
163  // accessories
164  } elseif ($findlog->accessory_id!='') {
165  $item = Accessory::find($findlog->accessory_id);
166  }
167 
168  // Check if the asset exists
169  if (is_null($item)) {
170  // Redirect to the asset management page
171  return Redirect::to('account')->with('error', trans('admin/hardware/message.does_not_exist'));
172  } elseif (!Company::isCurrentUserHasAccess($item)) {
173  return Redirect::route('requestable-assets')->with('error', trans('general.insufficient_permissions'));
174  } else {
175  return View::make('account/accept-asset', compact('item'))->with('findlog', $findlog);
176  }
177  }
static isCurrentUserHasAccess($companyable)
Definition: Company.php:96
App\Http\Controllers\ViewAssetsController::getIndex ( )

Redirect to the profile page.

Returns
Redirect

Definition at line 36 of file ViewAssetsController.php.

37  {
38 
39  $user = User::with('assets', 'assets.model', 'consumables', 'accessories', 'licenses', 'userloc')->withTrashed()->find(Auth::user()->id);
40 
41  $userlog = $user->userlog->load('assetlog', 'consumablelog', 'assetlog.model', 'licenselog', 'accessorylog', 'userlog', 'adminlog');
42 
43 
44 
45  if (isset($user->id)) {
46  return View::make('account/view-assets', compact('user', 'userlog'));
47  } else {
48  // Prepare the error message
49  $error = trans('admin/users/message.user_not_found', compact('id'));
50 
51  // Redirect to the user management page
52  return Redirect::route('users')->with('error', $error);
53  }
54 
55  }
App\Http\Controllers\ViewAssetsController::getRequestableIndex ( )

Definition at line 58 of file ViewAssetsController.php.

59  {
60 
61  $assets = Asset::with('model', 'defaultLoc')->Hardware()->RequestableAssets()->get();
62 
63  return View::make('account/requestable-assets', compact('user', 'assets'));
64  }
App\Http\Controllers\ViewAssetsController::getRequestAsset (   $assetId = null)

Definition at line 67 of file ViewAssetsController.php.

68  {
69 
70  $user = Auth::user();
71 
72  // Check if the asset exists and is requestable
73  if (is_null($asset = Asset::RequestableAssets()->find($assetId))) {
74  // Redirect to the asset management page
75  return Redirect::route('requestable-assets')->with('error', trans('admin/hardware/message.does_not_exist_or_not_requestable'));
76  } elseif (!Company::isCurrentUserHasAccess($asset)) {
77  return Redirect::route('requestable-assets')->with('error', trans('general.insufficient_permissions'));
78  } else {
79 
80  $logaction = new Actionlog();
81  $logaction->asset_id = $data['asset_id'] = $asset->id;
82  $logaction->asset_type = $data['asset_type'] = 'hardware';
83  $logaction->created_at = $data['requested_date'] = date("Y-m-d h:i:s");
84 
85  if ($user->location_id) {
86  $logaction->location_id = $user->location_id;
87  }
88  $logaction->user_id = $data['user_id'] = Auth::user()->id;
89  $log = $logaction->logaction('requested');
90 
91  $data['requested_by'] = $user->fullName();
92  $data['asset_name'] = $asset->showAssetName();
93 
94  $settings = Setting::getSettings();
95 
96  if (($settings->alert_email!='') && ($settings->alerts_enabled=='1') && (!config('app.lock_passwords'))) {
97  Mail::send('emails.asset-requested', $data, function ($m) use ($user, $settings) {
98  $m->to(explode(',', $settings->alert_email), $settings->site_name);
99  $m->subject('Asset Requested');
100  });
101  }
102 
103 
104  if ($settings->slack_endpoint) {
105 
106 
107  $slack_settings = [
108  'username' => $settings->botname,
109  'channel' => $settings->slack_channel,
110  'link_names' => true
111  ];
112 
113  $client = new \Maknz\Slack\Client($settings->slack_endpoint, $slack_settings);
114 
115  try {
116  $client->attach([
117  'color' => 'good',
118  'fields' => [
119  [
120  'title' => 'REQUESTED:',
121  'value' => strtoupper($logaction->asset_type).' asset <'.config('app.url').'/hardware/'.$asset->id.'/view'.'|'.$asset->showAssetName().'> requested by <'.config('app.url').'/hardware/'.$asset->id.'/view'.'|'.Auth::user()->fullName().'>.'
122  ]
123 
124  ]
125  ])->send('Asset Requested');
126 
127  } catch (Exception $e) {
128 
129  }
130 
131  }
132 
133  return Redirect::route('requestable-assets')->with('success')->with('success', trans('admin/hardware/message.requests.success'));
134  }
135 
136 
137  }
static isCurrentUserHasAccess($companyable)
Definition: Company.php:96
static getSettings()
Definition: Setting.php:33
App\Http\Controllers\ViewAssetsController::postAcceptAsset (   $logID = null)

Definition at line 180 of file ViewAssetsController.php.

181  {
182 
183  // Check if the asset exists
184  if (is_null($findlog = Actionlog::find($logID))) {
185  // Redirect to the asset management page
186  return Redirect::to('account/view-assets')->with('error', trans('admin/hardware/message.does_not_exist'));
187  }
188 
189  // NOTE: make sure the global scope is applied
190  $is_unauthorized = is_null(Actionlog::where('id', '=', $logID)->first());
191  if ($is_unauthorized) {
192  return Redirect::route('requestable-assets')->with('error', trans('general.insufficient_permissions'));
193  }
194 
195  if ($findlog->accepted_id!='') {
196  // Redirect to the asset management page
197  return Redirect::to('account/view-assets')->with('error', trans('admin/users/message.error.asset_already_accepted'));
198  }
199 
200  if (!Input::has('asset_acceptance')) {
201  return Redirect::to('account/view-assets')->with('error', trans('admin/users/message.error.accept_or_decline'));
202  }
203 
204  $user = Auth::user();
205 
206  if ($user->id != $findlog->checkedout_to) {
207  return Redirect::to('account/view-assets')->with('error', trans('admin/users/message.error.incorrect_user_accepted'));
208  }
209 
210  $logaction = new Actionlog();
211 
212  if (Input::get('asset_acceptance')=='accepted') {
213  $logaction_msg = 'accepted';
214  $accepted="accepted";
215  $return_msg = trans('admin/users/message.accepted');
216  } else {
217  $logaction_msg = 'declined';
218  $accepted="rejected";
219  $return_msg = trans('admin/users/message.declined');
220  }
221 
222  // Asset
223  if (($findlog->asset_id!='') && ($findlog->asset_type=='hardware')) {
224  $logaction->asset_id = $findlog->asset_id;
225  $logaction->accessory_id = null;
226  $logaction->asset_type = 'hardware';
227 
228  if (Input::get('asset_acceptance')!='accepted') {
229  DB::table('assets')
230  ->where('id', $findlog->asset_id)
231  ->update(array('assigned_to' => null));
232  }
233 
234 
235  // software
236  } elseif (($findlog->asset_id!='') && ($findlog->asset_type=='software')) {
237  $logaction->asset_id = $findlog->asset_id;
238  $logaction->accessory_id = null;
239  $logaction->asset_type = 'software';
240 
241  // accessories
242  } elseif ($findlog->accessory_id!='') {
243  $logaction->asset_id = null;
244  $logaction->accessory_id = $findlog->accessory_id;
245  $logaction->asset_type = 'accessory';
246  }
247 
248  $logaction->checkedout_to = $findlog->checkedout_to;
249 
250  $logaction->note = e(Input::get('note'));
251  $logaction->user_id = $user->id;
252  $logaction->accepted_at = date("Y-m-d h:i:s");
253  $log = $logaction->logaction($logaction_msg);
254 
255  $update_checkout = DB::table('asset_logs')
256  ->where('id', $findlog->id)
257  ->update(array('accepted_id' => $logaction->id));
258 
259  $affected_asset=$logaction->assetlog;
260  $affected_asset->accepted=$accepted;
261  $affected_asset->save();
262 
263  if ($update_checkout) {
264  return Redirect::to('account/view-assets')->with('success', $return_msg);
265 
266  } else {
267  return Redirect::to('account/view-assets')->with('error', 'Something went wrong ');
268  }
269  }

The documentation for this class was generated from the following file: