2016-03-25 01:18:05 -07:00
|
|
|
<?php
|
2021-06-10 13:15:52 -07:00
|
|
|
|
2016-03-25 01:18:05 -07:00
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
2019-03-13 20:12:03 -07:00
|
|
|
use App\Http\Requests\ImageUploadRequest;
|
2022-10-04 15:45:25 -07:00
|
|
|
use App\Models\Asset;
|
2016-10-29 05:50:55 -07:00
|
|
|
use App\Models\Setting;
|
2022-10-04 15:45:25 -07:00
|
|
|
use App\Models\User;
|
|
|
|
use App\Notifications\CurrentInventory;
|
2022-08-25 12:24:26 -07:00
|
|
|
use Illuminate\Support\Facades\Auth;
|
2023-03-18 11:58:09 -07:00
|
|
|
use Illuminate\Support\Facades\Gate;
|
2017-08-22 12:09:04 -07:00
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use Illuminate\Support\Facades\Hash;
|
2018-09-29 21:33:52 -07:00
|
|
|
use Illuminate\Support\Facades\Storage;
|
2019-03-13 20:12:03 -07:00
|
|
|
use Image;
|
|
|
|
use Redirect;
|
|
|
|
use View;
|
2016-03-25 01:18:05 -07:00
|
|
|
|
2016-04-07 13:21:09 -07:00
|
|
|
/**
|
|
|
|
* This controller handles all actions related to User Profiles for
|
|
|
|
* the Snipe-IT Asset Management application.
|
|
|
|
*
|
|
|
|
* @version v1.0
|
|
|
|
*/
|
2016-03-25 01:18:05 -07:00
|
|
|
class ProfileController extends Controller
|
|
|
|
{
|
|
|
|
/**
|
2021-06-10 13:15:52 -07:00
|
|
|
* Returns a view with the user's profile form for editing
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @since [v1.0]
|
|
|
|
* @return \Illuminate\Contracts\View\View
|
2016-12-19 22:00:50 -08:00
|
|
|
*/
|
2016-03-25 01:18:05 -07:00
|
|
|
public function getIndex()
|
|
|
|
{
|
|
|
|
$user = Auth::user();
|
2021-06-10 13:15:52 -07:00
|
|
|
|
2017-10-28 11:17:52 -07:00
|
|
|
return view('account/profile', compact('user'));
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-06-10 13:15:52 -07:00
|
|
|
* Validates and stores the user's update data.
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @since [v1.0]
|
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
2016-12-19 22:00:50 -08:00
|
|
|
*/
|
2017-12-06 14:42:14 -08:00
|
|
|
public function postIndex(ImageUploadRequest $request)
|
2016-03-25 01:18:05 -07:00
|
|
|
{
|
|
|
|
$user = Auth::user();
|
2018-07-24 12:42:16 -07:00
|
|
|
$user->first_name = $request->input('first_name');
|
2021-06-10 13:15:52 -07:00
|
|
|
$user->last_name = $request->input('last_name');
|
|
|
|
$user->website = $request->input('website');
|
|
|
|
$user->gravatar = $request->input('gravatar');
|
|
|
|
$user->skin = $request->input('skin');
|
|
|
|
$user->phone = $request->input('phone');
|
2019-04-18 11:13:50 -07:00
|
|
|
|
2021-06-10 13:15:52 -07:00
|
|
|
if (! config('app.lock_passwords')) {
|
2018-07-24 12:42:16 -07:00
|
|
|
$user->locale = $request->input('locale', 'en');
|
|
|
|
}
|
2016-03-25 01:18:05 -07:00
|
|
|
|
2021-06-10 13:15:52 -07:00
|
|
|
if ((Gate::allows('self.two_factor')) && ((Setting::getSettings()->two_factor_enabled == '1') && (! config('app.lock_passwords')))) {
|
2018-07-24 12:42:16 -07:00
|
|
|
$user->two_factor_optin = $request->input('two_factor_optin', '0');
|
|
|
|
}
|
|
|
|
|
2021-06-10 13:15:52 -07:00
|
|
|
if (Gate::allows('self.edit_location') && (! config('app.lock_passwords'))) {
|
|
|
|
$user->location_id = $request->input('location_id');
|
2016-10-29 05:50:55 -07:00
|
|
|
}
|
2016-03-25 01:18:05 -07:00
|
|
|
|
2023-01-21 18:05:20 -08:00
|
|
|
// Handle the avatar upload and/or delete if necessary
|
|
|
|
app('\App\Http\Requests\ImageUploadRequest')->handleImages($user, 600, 'avatar', 'avatars', 'avatar');
|
2018-09-29 21:33:52 -07:00
|
|
|
|
|
|
|
|
2016-03-25 01:18:05 -07:00
|
|
|
if ($user->save()) {
|
2016-04-28 21:06:41 -07:00
|
|
|
return redirect()->route('profile')->with('success', 'Account successfully updated');
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
2021-06-10 13:15:52 -07:00
|
|
|
|
2016-04-28 21:06:41 -07:00
|
|
|
return redirect()->back()->withInput()->withErrors($user->getErrors());
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
2017-01-24 17:37:07 -08:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a page with the API token generation interface.
|
|
|
|
*
|
|
|
|
* We created a controller method for this because closures aren't allowed
|
|
|
|
* in the routes file if you want to be able to cache the routes.
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @since [v4.0]
|
|
|
|
*/
|
2023-11-08 14:48:20 -08:00
|
|
|
public function api(): \Illuminate\Contracts\View\View
|
2021-06-10 13:15:52 -07:00
|
|
|
{
|
2022-01-13 01:33:27 -08:00
|
|
|
// Make sure the self.api permission has been granted
|
|
|
|
if (!Gate::allows('self.api')) {
|
|
|
|
abort(403);
|
|
|
|
}
|
|
|
|
|
2017-01-24 17:37:07 -08:00
|
|
|
return view('account/api');
|
|
|
|
}
|
2017-08-22 12:09:04 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* User change email page.
|
|
|
|
*
|
|
|
|
* @return View
|
|
|
|
*/
|
|
|
|
public function password()
|
|
|
|
{
|
|
|
|
$user = Auth::user();
|
2022-08-25 12:24:26 -07:00
|
|
|
|
2017-08-22 12:09:04 -07:00
|
|
|
return view('account/change-password', compact('user'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Users change password form processing page.
|
|
|
|
*
|
|
|
|
* @return Redirect
|
|
|
|
*/
|
|
|
|
public function passwordSave(Request $request)
|
|
|
|
{
|
|
|
|
if (config('app.lock_passwords')) {
|
2017-11-02 17:12:51 -07:00
|
|
|
return redirect()->route('account.password.index')->with('error', trans('admin/users/table.lock_passwords'));
|
2017-08-22 12:22:32 -07:00
|
|
|
}
|
2017-08-22 12:09:04 -07:00
|
|
|
|
2017-08-22 12:22:32 -07:00
|
|
|
$user = Auth::user();
|
2021-06-10 13:15:52 -07:00
|
|
|
if ($user->ldap_import == '1') {
|
2017-11-02 17:12:51 -07:00
|
|
|
return redirect()->route('account.password.index')->with('error', trans('admin/users/message.error.password_ldap'));
|
2017-08-22 12:22:32 -07:00
|
|
|
}
|
2017-08-22 12:09:04 -07:00
|
|
|
|
2021-06-10 13:15:52 -07:00
|
|
|
$rules = [
|
2017-08-22 12:22:32 -07:00
|
|
|
'current_password' => 'required',
|
2019-02-13 21:30:15 -08:00
|
|
|
'password' => Setting::passwordComplexityRulesSaving('store').'|confirmed',
|
2021-06-10 13:15:52 -07:00
|
|
|
];
|
2017-08-22 12:09:04 -07:00
|
|
|
|
2017-08-22 12:22:32 -07:00
|
|
|
$validator = \Validator::make($request->all(), $rules);
|
2023-11-22 15:00:30 -08:00
|
|
|
|
2021-06-10 13:15:52 -07:00
|
|
|
$validator->after(function ($validator) use ($request, $user) {
|
|
|
|
if (! Hash::check($request->input('current_password'), $user->password)) {
|
2022-10-26 12:18:29 -07:00
|
|
|
$validator->errors()->add('current_password', trans('validation.custom.hashed_pass'));
|
2017-08-22 12:09:04 -07:00
|
|
|
}
|
2020-11-02 20:03:26 -08:00
|
|
|
|
2020-11-02 20:07:39 -08:00
|
|
|
// This checks to make sure that the user's password isn't the same as their username,
|
|
|
|
// email address, first name or last name (see https://github.com/snipe/snipe-it/issues/8661)
|
|
|
|
// While this is handled via SaveUserRequest form request in other places, we have to do this manually
|
|
|
|
// here because we don't have the username, etc form fields available in the profile password change
|
|
|
|
// form.
|
|
|
|
|
|
|
|
// There may be a more elegant way to do this in the future.
|
|
|
|
|
2020-11-02 20:13:54 -08:00
|
|
|
// First let's see if that option is enabled in the settings
|
2021-06-10 13:15:52 -07:00
|
|
|
if (strpos(Setting::passwordComplexityRulesSaving('store'), 'disallow_same_pwd_as_user_fields') !== false) {
|
2020-11-02 20:13:54 -08:00
|
|
|
if (($request->input('password') == $user->username) ||
|
|
|
|
($request->input('password') == $user->email) ||
|
|
|
|
($request->input('password') == $user->first_name) ||
|
2021-06-10 13:15:52 -07:00
|
|
|
($request->input('password') == $user->last_name)) {
|
2020-11-02 20:13:54 -08:00
|
|
|
$validator->errors()->add('password', trans('validation.disallow_same_pwd_as_user_fields'));
|
|
|
|
}
|
2020-11-02 20:03:26 -08:00
|
|
|
}
|
2017-08-22 12:22:32 -07:00
|
|
|
});
|
2017-08-22 12:09:04 -07:00
|
|
|
|
2021-06-10 13:15:52 -07:00
|
|
|
if (! $validator->fails()) {
|
2022-08-25 12:24:26 -07:00
|
|
|
|
2023-11-22 15:00:30 -08:00
|
|
|
$user->password = Hash::make($request->input('password'));
|
|
|
|
// We have to use saveQuietly here because for some reason this method was calling the User Oserver twice :(
|
|
|
|
$user->saveQuietly();
|
|
|
|
|
2022-08-25 12:24:26 -07:00
|
|
|
// Log the user out of other devices
|
|
|
|
Auth::logoutOtherDevices($request->input('password'));
|
2023-11-22 15:00:30 -08:00
|
|
|
return redirect()->route('account')->with('success', trans('passwords.password_change'));
|
2017-08-22 12:09:04 -07:00
|
|
|
|
|
|
|
}
|
2021-06-10 13:15:52 -07:00
|
|
|
return redirect()->back()->withInput()->withErrors($validator);
|
2017-08-22 12:09:04 -07:00
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-10-25 20:10:59 -07:00
|
|
|
/**
|
2017-10-25 20:23:59 -07:00
|
|
|
* Save the menu state of open/closed when the user clicks on the hamburger
|
|
|
|
* menu.
|
2017-10-25 20:10:59 -07:00
|
|
|
*
|
2017-10-25 20:23:59 -07:00
|
|
|
* This URL is triggered via jquery in
|
|
|
|
* resources/views/layouts/default.blade.php
|
2017-10-25 20:10:59 -07:00
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @since [v4.0]
|
|
|
|
* @return View
|
|
|
|
*/
|
2021-06-10 13:15:52 -07:00
|
|
|
public function getMenuState(Request $request)
|
|
|
|
{
|
|
|
|
if ($request->input('state') == 'open') {
|
2017-10-25 20:10:59 -07:00
|
|
|
$request->session()->put('menu_state', 'open');
|
|
|
|
} else {
|
|
|
|
$request->session()->put('menu_state', 'closed');
|
|
|
|
}
|
|
|
|
}
|
2022-10-04 15:45:25 -07:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Print inventory
|
|
|
|
*
|
|
|
|
* @author A. Gianotto
|
|
|
|
* @since [v6.0.12]
|
2022-10-04 16:37:33 -07:00
|
|
|
* @return Illuminate\View\View
|
2022-10-04 15:45:25 -07:00
|
|
|
*/
|
|
|
|
public function printInventory()
|
|
|
|
{
|
2022-10-04 16:37:33 -07:00
|
|
|
$show_user = Auth::user();
|
2022-10-04 15:45:25 -07:00
|
|
|
|
2022-10-04 16:37:33 -07:00
|
|
|
return view('users/print')
|
|
|
|
->with('assets', Auth::user()->assets)
|
2022-10-04 15:45:25 -07:00
|
|
|
->with('licenses', $show_user->licenses()->get())
|
2022-10-04 16:37:33 -07:00
|
|
|
->with('accessories', $show_user->accessories()->get())
|
|
|
|
->with('consumables', $show_user->consumables()->get())
|
2022-10-04 15:45:25 -07:00
|
|
|
->with('show_user', $show_user)
|
|
|
|
->with('settings', Setting::getSettings());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Emails user a list of assigned assets
|
|
|
|
*
|
|
|
|
* @author A. Gianotto
|
|
|
|
* @since [v6.0.12]
|
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
|
|
*/
|
|
|
|
public function emailAssetList()
|
|
|
|
{
|
|
|
|
|
|
|
|
if (!$user = User::find(Auth::user()->id)) {
|
|
|
|
return redirect()->back()
|
|
|
|
->with('error', trans('admin/users/message.user_not_found', ['id' => $id]));
|
|
|
|
}
|
|
|
|
if (empty($user->email)) {
|
|
|
|
return redirect()->back()->with('error', trans('admin/users/message.user_has_no_email'));
|
|
|
|
}
|
|
|
|
|
|
|
|
$user->notify((new CurrentInventory($user)));
|
|
|
|
return redirect()->back()->with('success', trans('admin/users/general.user_notified'));
|
|
|
|
}
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|