mirror of
https://github.com/snipe/snipe-it.git
synced 2024-12-26 22:19:41 -08:00
e27065fe16
Signed-off-by: snipe <snipe@snipe.net> # Conflicts: # .all-contributorsrc # README.md # app/Console/Commands/ResetDemoSettings.php # app/Helpers/Helper.php # app/Http/Controllers/Api/AccessoriesController.php # app/Http/Controllers/Api/AssetsController.php # app/Http/Controllers/Api/CategoriesController.php # app/Http/Controllers/Api/ComponentsController.php # app/Http/Controllers/Api/ConsumablesController.php # app/Http/Controllers/Api/LocationsController.php # app/Http/Controllers/Api/StatuslabelsController.php # app/Http/Controllers/Api/SuppliersController.php # app/Http/Controllers/AssetMaintenancesController.php # app/Http/Controllers/Auth/ForgotPasswordController.php # app/Http/Controllers/DepreciationsController.php # app/Http/Controllers/ReportsController.php # app/Http/Controllers/SettingsController.php # app/Http/Requests/ImageUploadRequest.php # app/Http/Transformers/ActionlogsTransformer.php # app/Http/Transformers/DepreciationsTransformer.php # app/Listeners/CheckoutableListener.php # app/Models/Accessory.php # app/Models/Asset.php # app/Models/Company.php # app/Models/Ldap.php # app/Models/User.php # app/Presenters/AssetPresenter.php # app/Presenters/CategoryPresenter.php # composer.json # composer.lock # config/version.php # database/factories/AssetModelFactory.php # database/migrations/2020_10_22_233743_move_accessory_checkout_note_to_join_table.php # database/seeds/AssetModelSeeder.php # package-lock.json # public/css/build/AdminLTE.css # public/css/build/app.css # public/css/build/overrides.css # public/css/dist/all.css # public/css/dist/bootstrap-table.css # public/css/dist/skins/skin-black-dark.css # public/css/dist/skins/skin-black-dark.min.css # public/css/dist/skins/skin-black.css # public/css/dist/skins/skin-black.min.css # public/css/dist/skins/skin-blue-dark.css # public/css/dist/skins/skin-blue-dark.min.css # public/css/dist/skins/skin-blue.css # public/css/dist/skins/skin-blue.min.css # public/css/dist/skins/skin-contrast.css # public/css/dist/skins/skin-contrast.min.css # public/css/dist/skins/skin-green-dark.css # public/css/dist/skins/skin-green-dark.min.css # public/css/dist/skins/skin-green.css # public/css/dist/skins/skin-green.min.css # public/css/dist/skins/skin-orange-dark.css # public/css/dist/skins/skin-orange-dark.min.css # public/css/dist/skins/skin-orange.css # public/css/dist/skins/skin-orange.min.css # public/css/dist/skins/skin-purple-dark.css # public/css/dist/skins/skin-purple-dark.min.css # public/css/dist/skins/skin-purple.css # public/css/dist/skins/skin-purple.min.css # public/css/dist/skins/skin-red-dark.css # public/css/dist/skins/skin-red-dark.min.css # public/css/dist/skins/skin-red.css # public/css/dist/skins/skin-red.min.css # public/css/dist/skins/skin-yellow-dark.css # public/css/dist/skins/skin-yellow-dark.min.css # public/css/dist/skins/skin-yellow.css # public/css/dist/skins/skin-yellow.min.css # public/js/build/app.js # public/js/build/vendor.js # public/js/dist/all.js # public/js/dist/bootstrap-table.js # public/mix-manifest.json # resources/assets/js/vue.js # resources/lang/af/validation.php # resources/lang/ar/admin/settings/general.php # resources/lang/ar/validation.php # resources/lang/bg/admin/settings/general.php # resources/lang/bg/validation.php # resources/lang/cs/admin/settings/general.php # resources/lang/cs/validation.php # resources/lang/cy/help.php # resources/lang/cy/validation.php # resources/lang/da/admin/settings/general.php # resources/lang/da/validation.php # resources/lang/de/admin/settings/general.php # resources/lang/de/validation.php # resources/lang/el/validation.php # resources/lang/en-GB/admin/settings/general.php # resources/lang/en-GB/validation.php # resources/lang/en-ID/admin/hardware/table.php # resources/lang/en-ID/admin/settings/general.php # resources/lang/en-ID/validation.php # resources/lang/es-CO/admin/settings/general.php # resources/lang/es-CO/auth/message.php # resources/lang/es-CO/button.php # resources/lang/es-CO/help.php # resources/lang/es-CO/validation.php # resources/lang/es-ES/admin/settings/general.php # resources/lang/es-ES/auth/message.php # resources/lang/es-ES/button.php # resources/lang/es-ES/help.php # resources/lang/es-ES/validation.php # resources/lang/es-MX/admin/settings/general.php # resources/lang/es-MX/validation.php # resources/lang/es-VE/admin/settings/general.php # resources/lang/es-VE/auth/message.php # resources/lang/es-VE/button.php # resources/lang/es-VE/help.php # resources/lang/es-VE/validation.php # resources/lang/et/validation.php # resources/lang/fa/validation.php # resources/lang/fi/admin/settings/general.php # resources/lang/fi/validation.php # resources/lang/fil/validation.php # resources/lang/fr/admin/settings/general.php # resources/lang/fr/validation.php # resources/lang/ga-IE/validation.php # resources/lang/he/admin/settings/general.php # resources/lang/he/general.php # resources/lang/he/validation.php # resources/lang/hr/validation.php # resources/lang/hu/validation.php # resources/lang/id/validation.php # resources/lang/is/admin/categories/general.php # resources/lang/is/admin/companies/message.php # resources/lang/is/admin/companies/table.php # resources/lang/is/admin/components/general.php # resources/lang/is/admin/components/table.php # resources/lang/is/admin/consumables/table.php # resources/lang/is/admin/depreciations/general.php # resources/lang/is/admin/depreciations/message.php # resources/lang/is/admin/hardware/form.php # resources/lang/is/admin/hardware/general.php # resources/lang/is/admin/hardware/message.php # resources/lang/is/admin/hardware/table.php # resources/lang/is/admin/kits/general.php # resources/lang/is/admin/licenses/form.php # resources/lang/is/admin/licenses/general.php # resources/lang/is/admin/locations/table.php # resources/lang/is/admin/manufacturers/table.php # resources/lang/is/admin/reports/message.php # resources/lang/is/admin/settings/general.php # resources/lang/is/admin/settings/message.php # resources/lang/is/admin/statuslabels/message.php # resources/lang/is/admin/suppliers/message.php # resources/lang/is/admin/suppliers/table.php # resources/lang/is/admin/users/table.php # resources/lang/is/mail.php # resources/lang/is/validation.php # resources/lang/it/admin/settings/general.php # resources/lang/it/validation.php # resources/lang/iu/validation.php # resources/lang/ja/mail.php # resources/lang/ja/validation.php # resources/lang/ko/validation.php # resources/lang/lt/validation.php # resources/lang/lv/validation.php # resources/lang/mi/validation.php # resources/lang/mk/validation.php # resources/lang/ml-IN/validation.php # resources/lang/mn/validation.php # resources/lang/ms/validation.php # resources/lang/nl/admin/settings/general.php # resources/lang/nl/validation.php # resources/lang/no/validation.php # resources/lang/pl/admin/settings/general.php # resources/lang/pl/validation.php # resources/lang/pt-BR/admin/settings/general.php # resources/lang/pt-BR/mail.php # resources/lang/pt-BR/validation.php # resources/lang/pt-PT/validation.php # resources/lang/ro/validation.php # resources/lang/ru/validation.php # resources/lang/sl/validation.php # resources/lang/sr-CS/admin/settings/general.php # resources/lang/sr-CS/validation.php # resources/lang/sv-SE/admin/settings/general.php # resources/lang/sv-SE/auth/message.php # resources/lang/sv-SE/button.php # resources/lang/sv-SE/mail.php # resources/lang/sv-SE/validation.php # resources/lang/ta/validation.php # resources/lang/th/validation.php # resources/lang/tl/validation.php # resources/lang/tr/mail.php # resources/lang/tr/validation.php # resources/lang/uk/admin/accessories/table.php # resources/lang/uk/admin/asset_maintenances/message.php # resources/lang/uk/admin/asset_maintenances/table.php # resources/lang/uk/validation.php # resources/lang/ur-PK/validation.php # resources/lang/vi/admin/settings/general.php # resources/lang/vi/validation.php # resources/lang/zh-CN/admin/settings/general.php # resources/lang/zh-CN/validation.php # resources/lang/zh-HK/validation.php # resources/lang/zh-TW/validation.php # resources/lang/zu/validation.php # resources/views/partials/bootstrap-table.blade.php # resources/views/partials/forms/edit/company-select.blade.php # routes/api.php
540 lines
19 KiB
PHP
540 lines
19 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api;
|
|
|
|
use App\Helpers\Helper;
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\SaveUserRequest;
|
|
use App\Http\Transformers\AccessoriesTransformer;
|
|
use App\Http\Transformers\AssetsTransformer;
|
|
use App\Http\Transformers\LicensesTransformer;
|
|
use App\Http\Transformers\SelectlistTransformer;
|
|
use App\Http\Transformers\UsersTransformer;
|
|
use App\Models\Asset;
|
|
use App\Models\Company;
|
|
use App\Models\License;
|
|
use App\Models\User;
|
|
use Auth;
|
|
use Illuminate\Http\Request;
|
|
use App\Http\Requests\ImageUploadRequest;
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
class UsersController extends Controller
|
|
{
|
|
/**
|
|
* Display a listing of the resource.
|
|
*
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
* @since [v4.0]
|
|
*
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function index(Request $request)
|
|
{
|
|
$this->authorize('view', User::class);
|
|
|
|
$users = User::select([
|
|
'users.activated',
|
|
'users.address',
|
|
'users.avatar',
|
|
'users.city',
|
|
'users.company_id',
|
|
'users.country',
|
|
'users.created_at',
|
|
'users.deleted_at',
|
|
'users.department_id',
|
|
'users.email',
|
|
'users.employee_num',
|
|
'users.first_name',
|
|
'users.id',
|
|
'users.jobtitle',
|
|
'users.last_login',
|
|
'users.last_name',
|
|
'users.locale',
|
|
'users.location_id',
|
|
'users.manager_id',
|
|
'users.notes',
|
|
'users.permissions',
|
|
'users.phone',
|
|
'users.state',
|
|
'users.two_factor_enrolled',
|
|
'users.two_factor_optin',
|
|
'users.updated_at',
|
|
'users.username',
|
|
'users.zip',
|
|
'users.ldap_import',
|
|
|
|
])->with('manager', 'groups', 'userloc', 'company', 'department', 'assets', 'licenses', 'accessories', 'consumables')
|
|
->withCount('assets as assets_count', 'licenses as licenses_count', 'accessories as accessories_count', 'consumables as consumables_count');
|
|
$users = Company::scopeCompanyables($users);
|
|
|
|
if (($request->filled('deleted')) && ($request->input('deleted') == 'true')) {
|
|
$users = $users->onlyTrashed();
|
|
} elseif (($request->filled('all')) && ($request->input('all') == 'true')) {
|
|
$users = $users->withTrashed();
|
|
}
|
|
|
|
if ($request->filled('activated')) {
|
|
$users = $users->where('users.activated', '=', $request->input('activated'));
|
|
}
|
|
|
|
if ($request->filled('company_id')) {
|
|
$users = $users->where('users.company_id', '=', $request->input('company_id'));
|
|
}
|
|
|
|
if ($request->filled('location_id')) {
|
|
$users = $users->where('users.location_id', '=', $request->input('location_id'));
|
|
}
|
|
|
|
if ($request->filled('email')) {
|
|
$users = $users->where('users.email', '=', $request->input('email'));
|
|
}
|
|
|
|
if ($request->filled('username')) {
|
|
$users = $users->where('users.username', '=', $request->input('username'));
|
|
}
|
|
|
|
if ($request->filled('first_name')) {
|
|
$users = $users->where('users.first_name', '=', $request->input('first_name'));
|
|
}
|
|
|
|
if ($request->filled('last_name')) {
|
|
$users = $users->where('users.last_name', '=', $request->input('last_name'));
|
|
}
|
|
|
|
if ($request->filled('employee_num')) {
|
|
$users = $users->where('users.employee_num', '=', $request->input('employee_num'));
|
|
}
|
|
|
|
if ($request->filled('state')) {
|
|
$users = $users->where('users.state', '=', $request->input('state'));
|
|
}
|
|
|
|
if ($request->filled('country')) {
|
|
$users = $users->where('users.country', '=', $request->input('country'));
|
|
}
|
|
|
|
if ($request->filled('zip')) {
|
|
$users = $users->where('users.zip', '=', $request->input('zip'));
|
|
}
|
|
|
|
if ($request->filled('group_id')) {
|
|
$users = $users->ByGroup($request->get('group_id'));
|
|
}
|
|
|
|
if ($request->filled('department_id')) {
|
|
$users = $users->where('users.department_id', '=', $request->input('department_id'));
|
|
}
|
|
|
|
if ($request->filled('manager_id')) {
|
|
$users = $users->where('users.manager_id','=',$request->input('manager_id'));
|
|
}
|
|
|
|
if ($request->filled('search')) {
|
|
$users = $users->TextSearch($request->input('search'));
|
|
}
|
|
|
|
$order = $request->input('order') === 'asc' ? 'asc' : 'desc';
|
|
$offset = (($users) && (request('offset') > $users->count())) ? 0 : request('offset', 0);
|
|
|
|
// Set the offset to the API call's offset, unless the offset is higher than the actual count of items in which
|
|
// case we override with the actual count, so we should return 0 items.
|
|
$offset = (($users) && ($request->get('offset') > $users->count())) ? $users->count() : $request->get('offset', 0);
|
|
|
|
// Check to make sure the limit is not higher than the max allowed
|
|
((config('app.max_results') >= $request->input('limit')) && ($request->filled('limit'))) ? $limit = $request->input('limit') : $limit = config('app.max_results');
|
|
|
|
switch ($request->input('sort')) {
|
|
case 'manager':
|
|
$users = $users->OrderManager($order);
|
|
break;
|
|
case 'location':
|
|
$users = $users->OrderLocation($order);
|
|
break;
|
|
case 'department':
|
|
$users = $users->OrderDepartment($order);
|
|
break;
|
|
case 'company':
|
|
$users = $users->OrderCompany($order);
|
|
break;
|
|
default:
|
|
$allowed_columns =
|
|
[
|
|
'last_name', 'first_name', 'email', 'jobtitle', 'username', 'employee_num',
|
|
'assets', 'accessories', 'consumables', 'licenses', 'groups', 'activated', 'created_at',
|
|
'two_factor_enrolled', 'two_factor_optin', 'last_login', 'assets_count', 'licenses_count',
|
|
'consumables_count', 'accessories_count', 'phone', 'address', 'city', 'state',
|
|
'country', 'zip', 'id', 'ldap_import',
|
|
];
|
|
|
|
$sort = in_array($request->get('sort'), $allowed_columns) ? $request->get('sort') : 'first_name';
|
|
$users = $users->orderBy($sort, $order);
|
|
break;
|
|
}
|
|
|
|
$total = $users->count();
|
|
$users = $users->skip($offset)->take($limit)->get();
|
|
|
|
return (new UsersTransformer)->transformUsers($users, $total);
|
|
}
|
|
|
|
/**
|
|
* Gets a paginated collection for the select2 menus
|
|
*
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
* @since [v4.0.16]
|
|
* @see \App\Http\Transformers\SelectlistTransformer
|
|
*/
|
|
public function selectlist(Request $request)
|
|
{
|
|
$users = User::select(
|
|
[
|
|
'users.id',
|
|
'users.username',
|
|
'users.employee_num',
|
|
'users.first_name',
|
|
'users.last_name',
|
|
'users.gravatar',
|
|
'users.avatar',
|
|
'users.email',
|
|
]
|
|
)->where('show_in_list', '=', '1');
|
|
|
|
$users = Company::scopeCompanyables($users);
|
|
|
|
if ($request->filled('search')) {
|
|
$users = $users->SimpleNameSearch($request->get('search'))
|
|
->orWhere('username', 'LIKE', '%'.$request->get('search').'%')
|
|
->orWhere('employee_num', 'LIKE', '%'.$request->get('search').'%');
|
|
}
|
|
|
|
$users = $users->orderBy('last_name', 'asc')->orderBy('first_name', 'asc');
|
|
$users = $users->paginate(50);
|
|
|
|
foreach ($users as $user) {
|
|
$name_str = '';
|
|
if ($user->last_name != '') {
|
|
$name_str .= $user->last_name.', ';
|
|
}
|
|
$name_str .= $user->first_name;
|
|
|
|
if ($user->username != '') {
|
|
$name_str .= ' ('.$user->username.')';
|
|
}
|
|
|
|
if ($user->employee_num != '') {
|
|
$name_str .= ' - #'.$user->employee_num;
|
|
}
|
|
|
|
$user->use_text = $name_str;
|
|
$user->use_image = ($user->present()->gravatar) ? $user->present()->gravatar : null;
|
|
}
|
|
|
|
return (new SelectlistTransformer)->transformSelectlist($users);
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
* @since [v4.0]
|
|
* @param \Illuminate\Http\Request $request
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function store(SaveUserRequest $request)
|
|
{
|
|
$this->authorize('create', User::class);
|
|
|
|
$user = new User;
|
|
$user->fill($request->all());
|
|
|
|
if ($request->has('permissions')) {
|
|
$permissions_array = $request->input('permissions');
|
|
|
|
// Strip out the superuser permission if the API user isn't a superadmin
|
|
if (! Auth::user()->isSuperUser()) {
|
|
unset($permissions_array['superuser']);
|
|
}
|
|
$user->permissions = $permissions_array;
|
|
}
|
|
|
|
$tmp_pass = substr(str_shuffle('0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'), 0, 20);
|
|
$user->password = bcrypt($request->get('password', $tmp_pass));
|
|
|
|
app('App\Http\Requests\ImageUploadRequest')->handleImages($user, 600, 'image', 'avatars', 'avatar');
|
|
|
|
if ($user->save()) {
|
|
if ($request->filled('groups')) {
|
|
$user->groups()->sync($request->input('groups'));
|
|
} else {
|
|
$user->groups()->sync([]);
|
|
}
|
|
|
|
return response()->json(Helper::formatStandardApiResponse('success', (new UsersTransformer)->transformUser($user), trans('admin/users/message.success.create')));
|
|
}
|
|
|
|
return response()->json(Helper::formatStandardApiResponse('error', null, $user->getErrors()));
|
|
}
|
|
|
|
/**
|
|
* Display the specified resource.
|
|
*
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
* @param int $id
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function show($id)
|
|
{
|
|
$this->authorize('view', User::class);
|
|
$user = User::withCount('assets as assets_count', 'licenses as licenses_count', 'accessories as accessories_count', 'consumables as consumables_count')->findOrFail($id);
|
|
|
|
return (new UsersTransformer)->transformUser($user);
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
* @since [v4.0]
|
|
* @param \Illuminate\Http\Request $request
|
|
* @param int $id
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function update(SaveUserRequest $request, $id)
|
|
{
|
|
$this->authorize('update', User::class);
|
|
|
|
$user = User::findOrFail($id);
|
|
|
|
/**
|
|
* This is a janky hack to prevent people from changing admin demo user data on the public demo.
|
|
*
|
|
* The $ids 1 and 2 are special since they are seeded as superadmins in the demo seeder.
|
|
*
|
|
* Thanks, jerks. You are why we can't have nice things. - snipe
|
|
*
|
|
*/
|
|
|
|
|
|
if ((($id == 1) || ($id == 2)) && (config('app.lock_passwords'))) {
|
|
return response()->json(Helper::formatStandardApiResponse('error', null, 'Permission denied. You cannot update user information via API on the demo.'));
|
|
}
|
|
|
|
$user->fill($request->all());
|
|
|
|
if ($user->id == $request->input('manager_id')) {
|
|
return response()->json(Helper::formatStandardApiResponse('error', null, 'You cannot be your own manager'));
|
|
}
|
|
|
|
if ($request->filled('password')) {
|
|
$user->password = bcrypt($request->input('password'));
|
|
}
|
|
|
|
// We need to use has() instead of filled()
|
|
// here because we need to overwrite permissions
|
|
// if someone needs to null them out
|
|
if ($request->has('permissions')) {
|
|
$permissions_array = $request->input('permissions');
|
|
|
|
// Strip out the superuser permission if the API user isn't a superadmin
|
|
if (! Auth::user()->isSuperUser()) {
|
|
unset($permissions_array['superuser']);
|
|
}
|
|
$user->permissions = $permissions_array;
|
|
}
|
|
|
|
// 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)]);
|
|
|
|
|
|
app('App\Http\Requests\ImageUploadRequest')->handleImages($user, 600, 'image', 'avatars', 'avatar');
|
|
|
|
if ($user->save()) {
|
|
|
|
// Sync group memberships:
|
|
// This was changed in Snipe-IT v4.6.x to 4.7, since we upgraded to Laravel 5.5
|
|
// which changes the behavior of has vs filled.
|
|
// The $request->has method will now return true even if the input value is an empty string or null.
|
|
// A new $request->filled method has was added that provides the previous behavior of the has method.
|
|
|
|
// Check if the request has groups passed and has a value
|
|
if ($request->filled('groups')) {
|
|
$user->groups()->sync($request->input('groups'));
|
|
// The groups field has been passed but it is null, so we should blank it out
|
|
} elseif ($request->has('groups')) {
|
|
$user->groups()->sync([]);
|
|
}
|
|
|
|
return response()->json(Helper::formatStandardApiResponse('success', (new UsersTransformer)->transformUser($user), trans('admin/users/message.success.update')));
|
|
}
|
|
|
|
return response()->json(Helper::formatStandardApiResponse('error', null, $user->getErrors()));
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
* @since [v4.0]
|
|
* @param int $id
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function destroy($id)
|
|
{
|
|
$this->authorize('delete', User::class);
|
|
$user = User::findOrFail($id);
|
|
$this->authorize('delete', $user);
|
|
|
|
if (($user->assets) && ($user->assets->count() > 0)) {
|
|
return response()->json(Helper::formatStandardApiResponse('error', null, trans('admin/users/message.error.delete_has_assets')));
|
|
}
|
|
|
|
if (($user->licenses) && ($user->licenses->count() > 0)) {
|
|
return response()->json(Helper::formatStandardApiResponse('error', null, 'This user still has '.$user->licenses->count().' license(s) associated with them and cannot be deleted.'));
|
|
}
|
|
|
|
if (($user->accessories) && ($user->accessories->count() > 0)) {
|
|
return response()->json(Helper::formatStandardApiResponse('error', null, 'This user still has '.$user->accessories->count().' accessories associated with them.'));
|
|
}
|
|
|
|
if (($user->managedLocations()) && ($user->managedLocations()->count() > 0)) {
|
|
return response()->json(Helper::formatStandardApiResponse('error', null, 'This user still has '.$user->managedLocations()->count().' locations that they manage.'));
|
|
}
|
|
|
|
if ($user->delete()) {
|
|
|
|
// Remove the user's avatar if they have one
|
|
if (Storage::disk('public')->exists('avatars/'.$user->avatar)) {
|
|
try {
|
|
Storage::disk('public')->delete('avatars/'.$user->avatar);
|
|
} catch (\Exception $e) {
|
|
\Log::debug($e);
|
|
}
|
|
}
|
|
|
|
return response()->json(Helper::formatStandardApiResponse('success', null, trans('admin/users/message.success.delete')));
|
|
}
|
|
|
|
return response()->json(Helper::formatStandardApiResponse('error', null, trans('admin/users/message.error.delete')));
|
|
}
|
|
|
|
/**
|
|
* Return JSON containing a list of assets assigned to a user.
|
|
*
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
* @since [v3.0]
|
|
* @param $userId
|
|
* @return string JSON
|
|
*/
|
|
public function assets($id)
|
|
{
|
|
$this->authorize('view', User::class);
|
|
$this->authorize('view', Asset::class);
|
|
$assets = Asset::where('assigned_to', '=', $id)->where('assigned_type', '=', User::class)->with('model')->get();
|
|
|
|
return (new AssetsTransformer)->transformAssets($assets, $assets->count());
|
|
}
|
|
|
|
/**
|
|
* Return JSON containing a list of accessories assigned to a user.
|
|
*
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
* @since [v4.6.14]
|
|
* @param $userId
|
|
* @return string JSON
|
|
*/
|
|
public function accessories($id)
|
|
{
|
|
$this->authorize('view', User::class);
|
|
$user = User::findOrFail($id);
|
|
$this->authorize('view', Accessory::class);
|
|
$accessories = $user->accessories;
|
|
|
|
return (new AccessoriesTransformer)->transformAccessories($accessories, $accessories->count());
|
|
}
|
|
|
|
/**
|
|
* Return JSON containing a list of licenses assigned to a user.
|
|
*
|
|
* @author [N. Mathar] [<snipe@snipe.net>]
|
|
* @since [v5.0]
|
|
* @param $userId
|
|
* @return string JSON
|
|
*/
|
|
public function licenses($id)
|
|
{
|
|
$this->authorize('view', User::class);
|
|
$this->authorize('view', License::class);
|
|
$user = User::where('id', $id)->withTrashed()->first();
|
|
$licenses = $user->licenses()->get();
|
|
|
|
return (new LicensesTransformer())->transformLicenses($licenses, $licenses->count());
|
|
}
|
|
|
|
/**
|
|
* Reset the user's two-factor status
|
|
*
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
* @since [v3.0]
|
|
* @param $userId
|
|
* @return string JSON
|
|
*/
|
|
public function postTwoFactorReset(Request $request)
|
|
{
|
|
$this->authorize('update', User::class);
|
|
|
|
if ($request->filled('id')) {
|
|
try {
|
|
$user = User::find($request->get('id'));
|
|
$user->two_factor_secret = null;
|
|
$user->two_factor_enrolled = 0;
|
|
$user->save();
|
|
|
|
return response()->json(['message' => trans('admin/settings/general.two_factor_reset_success')], 200);
|
|
} catch (\Exception $e) {
|
|
return response()->json(['message' => trans('admin/settings/general.two_factor_reset_error')], 500);
|
|
}
|
|
}
|
|
|
|
return response()->json(['message' => 'No ID provided'], 500);
|
|
}
|
|
|
|
/**
|
|
* Get info on the current user.
|
|
*
|
|
* @author [Juan Font] [<juanfontalonso@gmail.com>]
|
|
* @since [v4.4.2]
|
|
* @param \Illuminate\Http\Request $request
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function getCurrentUserInfo(Request $request)
|
|
{
|
|
return (new UsersTransformer)->transformUser($request->user());
|
|
}
|
|
|
|
/**
|
|
* Restore a soft-deleted user.
|
|
*
|
|
* @author [E. Taylor] [<dev@evantaylor.name>]
|
|
* @param int $userId
|
|
* @since [v6.0.0]
|
|
* @return JsonResponse
|
|
*/
|
|
public function restore($userId = null)
|
|
{
|
|
// Get asset information
|
|
$user = User::withTrashed()->find($userId);
|
|
$this->authorize('delete', $user);
|
|
if (isset($user->id)) {
|
|
// Restore the user
|
|
User::withTrashed()->where('id', $userId)->restore();
|
|
|
|
return response()->json(Helper::formatStandardApiResponse('success', null, trans('admin/users/message.success.restored')));
|
|
}
|
|
|
|
$id = $userId;
|
|
return response()->json(Helper::formatStandardApiResponse('error', null, trans('admin/users/message.user_not_found', compact('id'))), 200);
|
|
}
|
|
}
|