2018-04-04 17:33:02 -07:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Api;
|
|
|
|
|
2019-03-13 20:12:03 -07:00
|
|
|
use App\Helpers\Helper;
|
2018-04-04 17:33:02 -07:00
|
|
|
use App\Http\Controllers\Controller;
|
2019-03-13 20:12:03 -07:00
|
|
|
use App\Models\CheckoutRequest;
|
2022-06-28 21:29:15 -07:00
|
|
|
use Illuminate\Http\Response;
|
|
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use Laravel\Passport\TokenRepository;
|
|
|
|
use Illuminate\Contracts\Validation\Factory as ValidationFactory;
|
2023-03-18 11:58:09 -07:00
|
|
|
use Illuminate\Support\Facades\Gate;
|
2023-10-25 11:42:33 -07:00
|
|
|
use App\Models\CustomField;
|
2022-06-28 23:10:56 -07:00
|
|
|
use DB;
|
2018-04-04 17:33:02 -07:00
|
|
|
|
|
|
|
class ProfileController extends Controller
|
|
|
|
{
|
2022-06-28 21:29:15 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The token repository implementation.
|
|
|
|
*
|
|
|
|
* @var \Laravel\Passport\TokenRepository
|
|
|
|
*/
|
|
|
|
protected $tokenRepository;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a controller instance.
|
|
|
|
*
|
|
|
|
* @param \Laravel\Passport\TokenRepository $tokenRepository
|
|
|
|
* @param \Illuminate\Contracts\Validation\Factory $validation
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function __construct(TokenRepository $tokenRepository, ValidationFactory $validation)
|
|
|
|
{
|
|
|
|
$this->validation = $validation;
|
|
|
|
$this->tokenRepository = $tokenRepository;
|
|
|
|
}
|
|
|
|
|
2018-04-04 17:33:02 -07:00
|
|
|
/**
|
|
|
|
* Display a listing of requested assets.
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @since [v4.3.0]
|
|
|
|
*
|
2021-06-10 13:15:52 -07:00
|
|
|
* @return array
|
2018-04-04 17:33:02 -07:00
|
|
|
*/
|
|
|
|
public function requestedAssets()
|
|
|
|
{
|
|
|
|
$checkoutRequests = CheckoutRequest::where('user_id', '=', Auth::user()->id)->get();
|
|
|
|
|
2023-10-25 11:42:33 -07:00
|
|
|
$results = array();
|
|
|
|
$show_field = array();
|
|
|
|
$showable_fields = array();
|
2018-04-04 17:33:02 -07:00
|
|
|
$results['total'] = $checkoutRequests->count();
|
|
|
|
|
2023-10-25 11:42:33 -07:00
|
|
|
$all_custom_fields = CustomField::all(); //used as a 'cache' of custom fields throughout this page load
|
|
|
|
foreach ($all_custom_fields as $field) {
|
|
|
|
if (($field->field_encrypted=='0') && ($field->show_in_requestable_list=='1')) {
|
|
|
|
$showable_fields[] = $field->db_column_name();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-04 17:33:02 -07:00
|
|
|
foreach ($checkoutRequests as $checkoutRequest) {
|
2019-03-05 23:46:40 -08:00
|
|
|
|
|
|
|
// Make sure the asset and request still exist
|
|
|
|
if ($checkoutRequest && $checkoutRequest->itemRequested()) {
|
2023-10-25 11:42:33 -07:00
|
|
|
$assets = [
|
2022-04-15 04:22:20 -07:00
|
|
|
'image' => e($checkoutRequest->itemRequested()->present()->getImageUrl()),
|
|
|
|
'name' => e($checkoutRequest->itemRequested()->present()->name()),
|
|
|
|
'type' => e($checkoutRequest->itemType()),
|
|
|
|
'qty' => (int) $checkoutRequest->quantity,
|
|
|
|
'location' => ($checkoutRequest->location()) ? e($checkoutRequest->location()->name) : null,
|
2019-03-05 23:46:40 -08:00
|
|
|
'expected_checkin' => Helper::getFormattedDateObject($checkoutRequest->itemRequested()->expected_checkin, 'datetime'),
|
|
|
|
'request_date' => Helper::getFormattedDateObject($checkoutRequest->created_at, 'datetime'),
|
|
|
|
];
|
2023-10-25 11:42:33 -07:00
|
|
|
|
|
|
|
foreach ($showable_fields as $showable_field_name) {
|
|
|
|
$show_field['custom_fields.'.$showable_field_name] = $checkoutRequest->itemRequested()->{$showable_field_name};
|
|
|
|
}
|
|
|
|
|
|
|
|
// Merge the plain asset data and the custom fields data
|
|
|
|
$results['rows'][] = array_merge($assets, $show_field);
|
2019-03-05 23:46:40 -08:00
|
|
|
}
|
2023-10-25 11:42:33 -07:00
|
|
|
|
|
|
|
|
2018-04-04 17:33:02 -07:00
|
|
|
}
|
2021-06-10 13:15:52 -07:00
|
|
|
|
2018-04-04 17:33:02 -07:00
|
|
|
return $results;
|
|
|
|
}
|
2022-06-28 21:29:15 -07:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete an API token
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @since [v6.0.5]
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
public function createApiToken(Request $request) {
|
|
|
|
|
2022-06-28 22:09:18 -07:00
|
|
|
if (!Gate::allows('self.api')) {
|
|
|
|
abort(403);
|
|
|
|
}
|
|
|
|
|
2022-06-28 21:29:15 -07:00
|
|
|
$accessTokenName = $request->input('name', 'Auth Token');
|
|
|
|
|
|
|
|
if ($accessToken = Auth::user()->createToken($accessTokenName)->accessToken) {
|
|
|
|
|
2022-06-28 23:10:56 -07:00
|
|
|
// Get the ID so we can return that with the payload
|
|
|
|
$token = DB::table('oauth_access_tokens')->where('user_id', '=', Auth::user()->id)->where('name','=',$accessTokenName)->orderBy('created_at', 'desc')->first();
|
|
|
|
$accessTokenData['id'] = $token->id;
|
|
|
|
$accessTokenData['token'] = $accessToken;
|
|
|
|
$accessTokenData['name'] = $accessTokenName;
|
|
|
|
return response()->json(Helper::formatStandardApiResponse('success', $accessTokenData, 'Personal access token '.$accessTokenName.' created successfully'));
|
|
|
|
}
|
2022-06-28 21:29:15 -07:00
|
|
|
return response()->json(Helper::formatStandardApiResponse('error', null, 'Token could not be created.'));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete an API token
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @since [v6.0.5]
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
public function deleteApiToken($tokenId) {
|
|
|
|
|
2022-06-28 22:09:18 -07:00
|
|
|
if (!Gate::allows('self.api')) {
|
|
|
|
abort(403);
|
|
|
|
}
|
|
|
|
|
2022-06-28 21:29:15 -07:00
|
|
|
$token = $this->tokenRepository->findForUser(
|
|
|
|
$tokenId, Auth::user()->getAuthIdentifier()
|
|
|
|
);
|
|
|
|
|
|
|
|
if (is_null($token)) {
|
|
|
|
return new Response('', 404);
|
|
|
|
}
|
|
|
|
|
|
|
|
$token->revoke();
|
|
|
|
|
|
|
|
return new Response('', Response::HTTP_NO_CONTENT);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show user's API tokens
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @since [v6.0.5]
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
2022-06-28 21:32:12 -07:00
|
|
|
public function showApiTokens(Request $request) {
|
2022-06-28 21:29:15 -07:00
|
|
|
|
2022-06-28 22:09:18 -07:00
|
|
|
if (!Gate::allows('self.api')) {
|
|
|
|
abort(403);
|
|
|
|
}
|
|
|
|
|
2022-06-28 21:29:15 -07:00
|
|
|
$tokens = $this->tokenRepository->forUser(Auth::user()->getAuthIdentifier());
|
2022-06-28 23:23:55 -07:00
|
|
|
$token_values = $tokens->load('client')->filter(function ($token) {
|
2022-06-28 21:29:15 -07:00
|
|
|
return $token->client->personal_access_client && ! $token->revoked;
|
|
|
|
})->values();
|
|
|
|
|
2022-06-28 23:23:55 -07:00
|
|
|
return response()->json(Helper::formatStandardApiResponse('success', $token_values, null));
|
|
|
|
|
2022-06-28 21:29:15 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-04-04 17:33:02 -07:00
|
|
|
}
|