2016-03-25 01:18:05 -07:00
|
|
|
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
|
|
|
use Image;
|
|
|
|
use Input;
|
|
|
|
use Redirect;
|
|
|
|
use View;
|
|
|
|
use Auth;
|
|
|
|
use App\Helpers\Helper;
|
2016-10-29 05:50:55 -07:00
|
|
|
use App\Models\Setting;
|
2016-10-31 16:52:25 -07:00
|
|
|
use Gate;
|
2017-08-22 12:09:04 -07:00
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use Illuminate\Support\Facades\Hash;
|
2017-12-06 14:42:14 -08:00
|
|
|
use App\Http\Requests\ImageUploadRequest;
|
2018-09-29 21:33:52 -07:00
|
|
|
use Illuminate\Support\Facades\Storage;
|
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
|
|
|
|
{
|
|
|
|
/**
|
2016-04-07 13:39:35 -07:00
|
|
|
* Returns a view with the user's profile form for editing
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @since [v1.0]
|
2016-12-19 22:00:50 -08:00
|
|
|
* @return \Illuminate\Contracts\View\View
|
|
|
|
*/
|
2016-03-25 01:18:05 -07:00
|
|
|
public function getIndex()
|
|
|
|
{
|
|
|
|
$user = Auth::user();
|
2017-10-28 11:17:52 -07:00
|
|
|
return view('account/profile', compact('user'));
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-04-07 13:39:35 -07:00
|
|
|
* Validates and stores the user's update data.
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @since [v1.0]
|
2016-12-19 22:00:50 -08:00
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
|
|
*/
|
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');
|
|
|
|
$user->last_name = $request->input('last_name');
|
|
|
|
$user->website = $request->input('website');
|
|
|
|
$user->gravatar = $request->input('gravatar');
|
|
|
|
|
|
|
|
if (!config('app.lock_passwords')) {
|
|
|
|
$user->locale = $request->input('locale', 'en');
|
|
|
|
}
|
2016-03-25 01:18:05 -07:00
|
|
|
|
2016-10-31 16:52:25 -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');
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2018-09-29 21:33:52 -07:00
|
|
|
|
|
|
|
if ($request->input('avatar_delete') == 1) {
|
2016-03-25 01:18:05 -07:00
|
|
|
$user->avatar = null;
|
|
|
|
}
|
|
|
|
|
2018-09-29 21:33:52 -07:00
|
|
|
|
|
|
|
if ($request->hasFile('avatar')) {
|
|
|
|
$path = 'avatars';
|
|
|
|
|
|
|
|
if(!Storage::disk('public')->exists($path)) Storage::disk('public')->makeDirectory($path, 775);
|
|
|
|
|
|
|
|
$upload = $image = $request->file('avatar');
|
|
|
|
$ext = $image->getClientOriginalExtension();
|
|
|
|
$file_name = 'avatar-'.str_random(18).'.'.$ext;
|
|
|
|
|
|
|
|
if ($image->getClientOriginalExtension()!='svg') {
|
|
|
|
$upload = Image::make($image->getRealPath())->resize(84, 84);
|
|
|
|
}
|
|
|
|
|
|
|
|
// This requires a string instead of an object, so we use ($string)
|
|
|
|
Storage::disk('public')->put($path.'/'.$file_name, (string)$upload->encode());
|
|
|
|
|
|
|
|
// Remove Current image if exists
|
|
|
|
if (($user->avatar) && (Storage::disk('public')->exists($path.'/'.$user->avatar))) {
|
|
|
|
Storage::disk('public')->delete($path.'/'.$user->avatar);
|
|
|
|
}
|
|
|
|
|
|
|
|
$user->avatar = $file_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
}
|
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]
|
|
|
|
* @return View
|
|
|
|
*/
|
|
|
|
public function api() {
|
|
|
|
return view('account/api');
|
|
|
|
}
|
2017-08-22 12:09:04 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* User change email page.
|
|
|
|
*
|
|
|
|
* @return View
|
|
|
|
*/
|
|
|
|
public function password()
|
|
|
|
{
|
|
|
|
$user = Auth::user();
|
|
|
|
return view('account/change-password', compact('user'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Users change password form processing page.
|
|
|
|
*
|
|
|
|
* @return Redirect
|
|
|
|
*/
|
|
|
|
public function passwordSave(Request $request)
|
|
|
|
{
|
2017-08-22 12:22:32 -07:00
|
|
|
|
2017-08-22 12:09:04 -07:00
|
|
|
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();
|
|
|
|
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
|
|
|
|
2017-08-22 12:22:32 -07:00
|
|
|
$rules = array(
|
|
|
|
'current_password' => 'required',
|
2017-08-22 22:41:35 -07:00
|
|
|
'password' => Setting::passwordComplexityRulesSaving('store'),
|
2017-08-22 12:22:32 -07:00
|
|
|
'password_confirm' => 'required|same:password',
|
|
|
|
);
|
2017-08-22 12:09:04 -07:00
|
|
|
|
2017-08-22 12:22:32 -07:00
|
|
|
$validator = \Validator::make($request->all(), $rules);
|
|
|
|
$validator->after(function($validator) use ($request, $user) {
|
2017-08-22 12:09:04 -07:00
|
|
|
|
2017-08-22 12:22:32 -07:00
|
|
|
if (!Hash::check($request->input('current_password'), $user->password)) {
|
|
|
|
$validator->errors()->add('current_password', trans('validation.hashed_pass'));
|
2017-08-22 12:09:04 -07:00
|
|
|
}
|
2017-08-22 12:22:32 -07:00
|
|
|
|
|
|
|
});
|
2017-08-22 12:09:04 -07:00
|
|
|
|
2017-08-22 12:22:32 -07:00
|
|
|
if (!$validator->fails()) {
|
|
|
|
$user->password = Hash::make($request->input('password'));
|
|
|
|
$user->save();
|
|
|
|
return redirect()->route('account.password.index')->with('success', 'Password updated!');
|
2017-08-22 12:09:04 -07:00
|
|
|
|
|
|
|
}
|
2017-08-22 12:22:32 -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
|
|
|
|
*/
|
|
|
|
|
|
|
|
public function getMenuState(Request $request) {
|
|
|
|
if ($request->input('state')=='open') {
|
|
|
|
$request->session()->put('menu_state', 'open');
|
|
|
|
} else {
|
|
|
|
$request->session()->put('menu_state', 'closed');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-22 12:22:32 -07:00
|
|
|
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|