1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 07:02:34 +01:00
invoiceninja/app/Http/Controllers/UserController.php

359 lines
11 KiB
PHP
Raw Normal View History

2015-03-17 02:30:56 +01:00
<?php namespace App\Http\Controllers;
2015-03-16 22:45:25 +01:00
2015-03-24 08:24:44 +01:00
use Auth;
use Input;
2015-03-23 07:52:01 +01:00
use View;
2015-06-16 21:35:35 +02:00
use Request;
2015-03-24 08:24:44 +01:00
use Redirect;
2015-03-27 02:09:13 +01:00
use Session;
2015-04-06 07:45:27 +02:00
use URL;
2016-03-13 19:33:11 +01:00
use Password;
2015-04-01 21:57:02 +02:00
use Utils;
2015-04-06 07:45:27 +02:00
use Validator;
2015-04-01 21:57:02 +02:00
use App\Models\User;
use App\Ninja\Repositories\AccountRepository;
use App\Ninja\Mailers\ContactMailer;
use App\Ninja\Mailers\UserMailer;
2015-11-05 23:37:04 +01:00
use App\Services\UserService;
2015-03-16 22:45:25 +01:00
class UserController extends BaseController
{
protected $accountRepo;
protected $contactMailer;
protected $userMailer;
2015-11-05 23:37:04 +01:00
protected $userService;
2015-03-16 22:45:25 +01:00
2015-11-05 23:37:04 +01:00
public function __construct(AccountRepository $accountRepo, ContactMailer $contactMailer, UserMailer $userMailer, UserService $userService)
2015-03-16 22:45:25 +01:00
{
2016-03-02 14:36:42 +01:00
//parent::__construct();
2015-03-16 22:45:25 +01:00
$this->accountRepo = $accountRepo;
$this->contactMailer = $contactMailer;
$this->userMailer = $userMailer;
2015-11-05 23:37:04 +01:00
$this->userService = $userService;
2015-03-16 22:45:25 +01:00
}
2015-10-21 13:11:08 +02:00
public function index()
{
return Redirect::to('settings/' . ACCOUNT_USER_MANAGEMENT);
}
2015-03-16 22:45:25 +01:00
public function getDatatable()
{
2015-11-05 23:37:04 +01:00
return $this->userService->getDatatable(Auth::user()->account_id);
2015-03-16 22:45:25 +01:00
}
public function setTheme()
{
$user = User::find(Auth::user()->id);
$user->theme_id = Input::get('theme_id');
$user->save();
return Redirect::to(Input::get('path'));
}
public function forcePDFJS()
{
$user = Auth::user();
$user->force_pdfjs = true;
$user->save();
2015-07-29 21:55:12 +02:00
Session::flash('message', trans('texts.updated_settings'));
2015-03-16 22:45:25 +01:00
return Redirect::to('/dashboard');
}
public function edit($publicId)
{
$user = User::where('account_id', '=', Auth::user()->account_id)
->where('public_id', '=', $publicId)->firstOrFail();
$data = [
'user' => $user,
'method' => 'PUT',
'url' => 'users/'.$publicId,
];
return View::make('users.edit', $data);
}
public function update($publicId)
{
return $this->save($publicId);
}
public function store()
{
return $this->save();
}
/**
* Displays the form for account creation
*
*/
public function create()
{
2016-07-11 19:08:43 +02:00
if ( ! Auth::user()->registered) {
2015-03-16 22:45:25 +01:00
Session::flash('error', trans('texts.register_to_add_user'));
2015-10-14 16:15:39 +02:00
return Redirect::to('settings/' . ACCOUNT_USER_MANAGEMENT);
2015-11-05 23:37:04 +01:00
}
2016-07-11 19:08:43 +02:00
if ( ! Auth::user()->confirmed) {
2015-07-29 21:55:12 +02:00
Session::flash('error', trans('texts.confirmation_required'));
2015-10-14 16:15:39 +02:00
return Redirect::to('settings/' . ACCOUNT_USER_MANAGEMENT);
2015-03-16 22:45:25 +01:00
}
2016-07-11 19:08:43 +02:00
if (Utils::isNinja() && ! Auth::user()->caddAddUsers()) {
Session::flash('error', trans('texts.max_users_reached'));
return Redirect::to('settings/' . ACCOUNT_USER_MANAGEMENT);
2015-03-16 22:45:25 +01:00
}
$data = [
'user' => null,
'method' => 'POST',
'url' => 'users',
];
return View::make('users.edit', $data);
}
2015-11-05 23:37:04 +01:00
public function bulk()
2015-03-16 22:45:25 +01:00
{
2015-11-05 23:37:04 +01:00
$action = Input::get('bulk_action');
$id = Input::get('bulk_public_id');
2016-03-02 14:36:42 +01:00
2015-03-16 22:45:25 +01:00
$user = User::where('account_id', '=', Auth::user()->account_id)
2015-11-05 23:37:04 +01:00
->where('public_id', '=', $id)
->withTrashed()
->firstOrFail();
if ($action === 'archive') {
$user->delete();
} else {
2016-07-11 19:08:43 +02:00
if ( ! Auth::user()->caddAddUsers()) {
return Redirect::to('settings/' . ACCOUNT_USER_MANAGEMENT)
->with('error', trans('texts.max_users_reached'));
}
2015-11-05 23:37:04 +01:00
$user->restore();
}
2015-03-16 22:45:25 +01:00
2015-11-05 23:37:04 +01:00
Session::flash('message', trans("texts.{$action}d_user"));
2015-03-16 22:45:25 +01:00
2015-10-14 16:15:39 +02:00
return Redirect::to('settings/' . ACCOUNT_USER_MANAGEMENT);
2015-03-16 22:45:25 +01:00
}
/**
* Stores new account
*
*/
public function save($userPublicId = false)
{
if (Auth::user()->hasFeature(FEATURE_USERS)) {
2015-03-16 22:45:25 +01:00
$rules = [
'first_name' => 'required',
'last_name' => 'required',
];
if ($userPublicId) {
$user = User::where('account_id', '=', Auth::user()->account_id)
->where('public_id', '=', $userPublicId)->firstOrFail();
$rules['email'] = 'required|email|unique:users,email,'.$user->id.',id';
} else {
$rules['email'] = 'required|email|unique:users';
}
$validator = Validator::make(Input::all(), $rules);
if ($validator->fails()) {
return Redirect::to($userPublicId ? 'users/edit' : 'users/create')->withInput()->withErrors($validator);
}
if ($userPublicId) {
$user->first_name = trim(Input::get('first_name'));
$user->last_name = trim(Input::get('last_name'));
$user->username = trim(Input::get('email'));
$user->email = trim(Input::get('email'));
if (Auth::user()->hasFeature(FEATURE_USER_PERMISSIONS)) {
$user->is_admin = boolval(Input::get('is_admin'));
$user->permissions = Input::get('permissions');
}
2015-03-16 22:45:25 +01:00
} else {
$lastUser = User::withTrashed()->where('account_id', '=', Auth::user()->account_id)
->orderBy('public_id', 'DESC')->first();
$user = new User();
$user->account_id = Auth::user()->account_id;
$user->first_name = trim(Input::get('first_name'));
$user->last_name = trim(Input::get('last_name'));
$user->username = trim(Input::get('email'));
$user->email = trim(Input::get('email'));
$user->registered = true;
$user->password = str_random(RANDOM_KEY_LENGTH);
2015-04-05 21:15:37 +02:00
$user->confirmation_code = str_random(RANDOM_KEY_LENGTH);
2015-03-16 22:45:25 +01:00
$user->public_id = $lastUser->public_id + 1;
if (Auth::user()->hasFeature(FEATURE_USER_PERMISSIONS)) {
$user->is_admin = boolval(Input::get('is_admin'));
$user->permissions = Input::get('permissions');
}
2015-03-16 22:45:25 +01:00
}
$user->save();
if (!$user->confirmed) {
$this->userMailer->sendConfirmation($user, Auth::user());
$message = trans('texts.sent_invite');
} else {
$message = trans('texts.updated_user');
}
Session::flash('message', $message);
}
2016-03-02 14:36:42 +01:00
2015-10-14 16:15:39 +02:00
return Redirect::to('settings/' . ACCOUNT_USER_MANAGEMENT);
2015-03-16 22:45:25 +01:00
}
public function sendConfirmation($userPublicId)
{
$user = User::where('account_id', '=', Auth::user()->account_id)
->where('public_id', '=', $userPublicId)->firstOrFail();
$this->userMailer->sendConfirmation($user, Auth::user());
Session::flash('message', trans('texts.sent_invite'));
2015-10-14 16:15:39 +02:00
return Redirect::to('settings/' . ACCOUNT_USER_MANAGEMENT);
2015-03-16 22:45:25 +01:00
}
/**
* Attempt to confirm account with code
*
* @param string $code
*/
2016-03-13 19:33:11 +01:00
public function confirm($code)
2015-03-16 22:45:25 +01:00
{
2015-04-05 21:15:37 +02:00
$user = User::where('confirmation_code', '=', $code)->get()->first();
2016-03-02 14:36:42 +01:00
2015-04-05 21:15:37 +02:00
if ($user) {
$notice_msg = trans('texts.security.confirmation');
2015-03-16 22:45:25 +01:00
2015-04-05 21:15:37 +02:00
$user->confirmed = true;
2015-03-16 22:45:25 +01:00
$user->confirmation_code = '';
$user->save();
if ($user->public_id) {
2015-05-12 11:36:32 +02:00
//Auth::login($user);
2016-03-13 19:33:11 +01:00
$token = Password::getRepository()->create($user);
2015-03-16 22:45:25 +01:00
2015-05-12 11:36:32 +02:00
return Redirect::to("/password/reset/{$token}");
2016-07-11 19:08:43 +02:00
} else {
if (Auth::check()) {
if (Session::has(REQUESTED_PRO_PLAN)) {
Session::forget(REQUESTED_PRO_PLAN);
$url = '/settings/account_management?upgrade=true';
} else {
$url = '/dashboard';
}
2015-03-16 22:45:25 +01:00
} else {
$url = '/login';
2015-03-16 22:45:25 +01:00
}
return Redirect::to($url)->with('message', $notice_msg);
2015-03-16 22:45:25 +01:00
}
} else {
$error_msg = trans('texts.security.wrong_confirmation');
2015-03-16 22:45:25 +01:00
2015-04-05 21:15:37 +02:00
return Redirect::to('/login')->with('error', $error_msg);
2015-03-16 22:45:25 +01:00
}
}
/**
* Log the user out of the application.
*
*/
2015-07-07 22:08:16 +02:00
/*
2015-03-16 22:45:25 +01:00
public function logout()
{
if (Auth::check()) {
if (!Auth::user()->registered) {
$account = Auth::user()->account;
2015-07-07 22:08:16 +02:00
$this->accountRepo->unlinkAccount($account);
if ($account->company->accounts->count() == 1) {
2016-05-15 22:16:08 +02:00
$account->company->forceDelete();
}
2015-03-16 22:45:25 +01:00
$account->forceDelete();
}
}
2015-03-29 14:37:42 +02:00
Auth::logout();
2015-06-16 21:35:35 +02:00
Session::flush();
2015-03-16 22:45:25 +01:00
return Redirect::to('/')->with('clearGuestKey', true);
}
2015-07-07 22:08:16 +02:00
*/
2016-03-02 14:36:42 +01:00
2015-03-16 22:45:25 +01:00
public function changePassword()
{
// check the current password is correct
if (!Auth::validate([
'email' => Auth::user()->email,
'password' => Input::get('current_password')
])) {
return trans('texts.password_error_incorrect');
}
// validate the new password
$password = Input::get('new_password');
$confirm = Input::get('confirm_password');
if (strlen($password) < 6 || $password != $confirm) {
return trans('texts.password_error_invalid');
}
// save the new password
$user = Auth::user();
2015-04-13 17:05:34 +02:00
$user->password = bcrypt($password);
2015-03-16 22:45:25 +01:00
$user->save();
return RESULT_SUCCESS;
}
2015-06-16 21:35:35 +02:00
2015-09-01 20:40:30 +02:00
public function switchAccount($newUserId)
2015-06-16 21:35:35 +02:00
{
$oldUserId = Auth::user()->id;
$referer = Request::header('referer');
$account = $this->accountRepo->findUserAccounts($newUserId, $oldUserId);
2016-03-02 14:36:42 +01:00
2015-06-16 21:35:35 +02:00
if ($account) {
if ($account->hasUserId($newUserId) && $account->hasUserId($oldUserId)) {
Auth::loginUsingId($newUserId);
Auth::user()->account->loadLocalizationSettings();
2015-07-07 22:08:16 +02:00
// regenerate token to prevent open pages
// from saving under the wrong account
Session::put('_token', str_random(40));
2015-06-16 21:35:35 +02:00
}
}
2016-03-02 14:36:42 +01:00
2015-06-16 21:35:35 +02:00
return Redirect::to($referer);
}
public function unlinkAccount($userAccountId, $userId)
{
2015-07-07 22:08:16 +02:00
$this->accountRepo->unlinkUser($userAccountId, $userId);
2015-06-16 21:35:35 +02:00
$referer = Request::header('referer');
$users = $this->accountRepo->loadAccounts(Auth::user()->id);
Session::put(SESSION_USER_ACCOUNTS, $users);
Session::flash('message', trans('texts.unlinked_account'));
2015-08-03 09:15:58 +02:00
return Redirect::to('/dashboard');
}
public function manageCompanies()
{
2015-08-03 21:08:07 +02:00
return View::make('users.account_management');
2015-06-16 21:35:35 +02:00
}
2015-03-16 22:45:25 +01:00
}