2016-03-25 01:18:05 -07:00
< ? php
namespace App\Models ;
2016-12-23 17:52:00 -08:00
use App\Presenters\Presentable ;
2016-03-25 01:18:05 -07:00
use Illuminate\Auth\Authenticatable ;
use Illuminate\Auth\Passwords\CanResetPassword ;
2017-10-27 18:01:11 -07:00
use Illuminate\Foundation\Auth\Access\Authorizable ;
2016-03-25 01:18:05 -07:00
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract ;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract ;
2017-10-27 18:01:11 -07:00
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract ;
2016-03-25 01:18:05 -07:00
use Watson\Validating\ValidatingTrait ;
use Illuminate\Database\Eloquent\SoftDeletes ;
2016-07-26 01:39:30 -07:00
use App\Http\Traits\UniqueUndeletedTrait ;
2016-12-14 05:06:51 -08:00
use Illuminate\Notifications\Notifiable ;
2016-12-14 10:06:05 -08:00
use Laravel\Passport\HasApiTokens ;
2017-12-11 22:31:07 -08:00
use DB ;
2016-03-25 01:18:05 -07:00
2016-12-23 17:52:00 -08:00
class User extends SnipeModel implements AuthenticatableContract , CanResetPasswordContract
2016-03-25 01:18:05 -07:00
{
2016-12-23 17:52:00 -08:00
protected $presenter = 'App\Presenters\UserPresenter' ;
use SoftDeletes , ValidatingTrait ;
2017-10-27 18:01:11 -07:00
use Authenticatable , Authorizable , CanResetPassword , HasApiTokens ;
2016-07-26 01:39:30 -07:00
use UniqueUndeletedTrait ;
2016-12-14 05:06:51 -08:00
use Notifiable ;
2016-12-23 17:52:00 -08:00
use Presentable ;
2016-03-25 01:18:05 -07:00
protected $dates = [ 'deleted_at' ];
2017-08-03 19:50:18 -07:00
protected $hidden = [ 'password' , 'remember_token' , 'permissions' , 'reset_password_code' , 'persist_code' ];
2016-03-25 01:18:05 -07:00
protected $table = 'users' ;
protected $injectUniqueIdentifier = true ;
Importer mapping - v1 (#3677)
* Move importer to an inline-template, allows for translations and easier passing of data from laravel to vue.
* Pull the modal out into a dedicated partial, move importer to views/importer.
* Add document of CSV->importer mappings. Reorganize some code.
Progress.
* Add header_row and first_row to imports table, and process upon uploading a file
* Use an expandable table row instead of a modal for import processing. This should allow for field mapping interaction easier.
* Fix import processing after moving method.
* Frontend importer mapping improvements.
Invert display so we show found columns and allow users to select an
importer field to map to. Also implement sample data based on first row
of csv.
* Update select2. Maintain selected items properly.
* Backend support for importing. Only works on the web importer currently. Definitely needs testing and polish.
* We no longer use vue-modal plugin.
* Add a column to track field mappings to the imports table.
* Cleanup/rename methods+refactor
* Save field mappings and import type when attempting an import, and repopulate these values when returning to the page.
* Update debugbar to fix a bug in the debugbar code.
* Fix asset tag detection.
Also rename findMatch to be a bit clearer as to what it does.
Remove logging to file of imports for http imports because
it eats an incredible amouint of memory.
This commit also moves imports out of the hardware namespace and into
their own webcontroller and route prefix, remove dead code from
AssetController as a result.
* Dynamically limit options for select2 based on import type selected, and group them by item type.
* Add user importer.
Still need to implement emailing of passwords to new users, and probably
test a bit more.
This also bumps the memory limit for web imports up as well, I need to
profile memory usage here before too long.
* Query the db to find user matches rather than search the array. Performance is much much better.
* Speed/memory improvements in importers.
Move to querying the db rather than maintaining an array for all
importers. Also only store the id of items when we import, rather than
the full model. It saves a decent amount of memory.
* Remove grouping of items in select2
With the values being set dynamically, the grouping is redundant. It
also caused a regression with automatically guessing/matching field
names. This is starting to get close.
* Remove debug line on every create.
* Switch migration to be text field instead of json field for compatibility with older mysql/mariadb
* Fix asset import regression matching email address.
* Rearrange travis order in attempt to fix null settings.
* Use auth::id instead of fetching it off the user. Fixes a null object reference during seeding.
2017-06-21 16:37:37 -07:00
protected $fillable = [
2018-02-24 19:01:34 -08:00
'activated' ,
'address' ,
'city' ,
Importer mapping - v1 (#3677)
* Move importer to an inline-template, allows for translations and easier passing of data from laravel to vue.
* Pull the modal out into a dedicated partial, move importer to views/importer.
* Add document of CSV->importer mappings. Reorganize some code.
Progress.
* Add header_row and first_row to imports table, and process upon uploading a file
* Use an expandable table row instead of a modal for import processing. This should allow for field mapping interaction easier.
* Fix import processing after moving method.
* Frontend importer mapping improvements.
Invert display so we show found columns and allow users to select an
importer field to map to. Also implement sample data based on first row
of csv.
* Update select2. Maintain selected items properly.
* Backend support for importing. Only works on the web importer currently. Definitely needs testing and polish.
* We no longer use vue-modal plugin.
* Add a column to track field mappings to the imports table.
* Cleanup/rename methods+refactor
* Save field mappings and import type when attempting an import, and repopulate these values when returning to the page.
* Update debugbar to fix a bug in the debugbar code.
* Fix asset tag detection.
Also rename findMatch to be a bit clearer as to what it does.
Remove logging to file of imports for http imports because
it eats an incredible amouint of memory.
This commit also moves imports out of the hardware namespace and into
their own webcontroller and route prefix, remove dead code from
AssetController as a result.
* Dynamically limit options for select2 based on import type selected, and group them by item type.
* Add user importer.
Still need to implement emailing of passwords to new users, and probably
test a bit more.
This also bumps the memory limit for web imports up as well, I need to
profile memory usage here before too long.
* Query the db to find user matches rather than search the array. Performance is much much better.
* Speed/memory improvements in importers.
Move to querying the db rather than maintaining an array for all
importers. Also only store the id of items when we import, rather than
the full model. It saves a decent amount of memory.
* Remove grouping of items in select2
With the values being set dynamically, the grouping is redundant. It
also caused a regression with automatically guessing/matching field
names. This is starting to get close.
* Remove debug line on every create.
* Switch migration to be text field instead of json field for compatibility with older mysql/mariadb
* Fix asset import regression matching email address.
* Rearrange travis order in attempt to fix null settings.
* Use auth::id instead of fetching it off the user. Fixes a null object reference during seeding.
2017-06-21 16:37:37 -07:00
'company_id' ,
2018-02-24 19:01:34 -08:00
'country' ,
Importer mapping - v1 (#3677)
* Move importer to an inline-template, allows for translations and easier passing of data from laravel to vue.
* Pull the modal out into a dedicated partial, move importer to views/importer.
* Add document of CSV->importer mappings. Reorganize some code.
Progress.
* Add header_row and first_row to imports table, and process upon uploading a file
* Use an expandable table row instead of a modal for import processing. This should allow for field mapping interaction easier.
* Fix import processing after moving method.
* Frontend importer mapping improvements.
Invert display so we show found columns and allow users to select an
importer field to map to. Also implement sample data based on first row
of csv.
* Update select2. Maintain selected items properly.
* Backend support for importing. Only works on the web importer currently. Definitely needs testing and polish.
* We no longer use vue-modal plugin.
* Add a column to track field mappings to the imports table.
* Cleanup/rename methods+refactor
* Save field mappings and import type when attempting an import, and repopulate these values when returning to the page.
* Update debugbar to fix a bug in the debugbar code.
* Fix asset tag detection.
Also rename findMatch to be a bit clearer as to what it does.
Remove logging to file of imports for http imports because
it eats an incredible amouint of memory.
This commit also moves imports out of the hardware namespace and into
their own webcontroller and route prefix, remove dead code from
AssetController as a result.
* Dynamically limit options for select2 based on import type selected, and group them by item type.
* Add user importer.
Still need to implement emailing of passwords to new users, and probably
test a bit more.
This also bumps the memory limit for web imports up as well, I need to
profile memory usage here before too long.
* Query the db to find user matches rather than search the array. Performance is much much better.
* Speed/memory improvements in importers.
Move to querying the db rather than maintaining an array for all
importers. Also only store the id of items when we import, rather than
the full model. It saves a decent amount of memory.
* Remove grouping of items in select2
With the values being set dynamically, the grouping is redundant. It
also caused a regression with automatically guessing/matching field
names. This is starting to get close.
* Remove debug line on every create.
* Switch migration to be text field instead of json field for compatibility with older mysql/mariadb
* Fix asset import regression matching email address.
* Rearrange travis order in attempt to fix null settings.
* Use auth::id instead of fetching it off the user. Fixes a null object reference during seeding.
2017-06-21 16:37:37 -07:00
'department_id' ,
2018-02-24 19:01:34 -08:00
'email' ,
Importer mapping - v1 (#3677)
* Move importer to an inline-template, allows for translations and easier passing of data from laravel to vue.
* Pull the modal out into a dedicated partial, move importer to views/importer.
* Add document of CSV->importer mappings. Reorganize some code.
Progress.
* Add header_row and first_row to imports table, and process upon uploading a file
* Use an expandable table row instead of a modal for import processing. This should allow for field mapping interaction easier.
* Fix import processing after moving method.
* Frontend importer mapping improvements.
Invert display so we show found columns and allow users to select an
importer field to map to. Also implement sample data based on first row
of csv.
* Update select2. Maintain selected items properly.
* Backend support for importing. Only works on the web importer currently. Definitely needs testing and polish.
* We no longer use vue-modal plugin.
* Add a column to track field mappings to the imports table.
* Cleanup/rename methods+refactor
* Save field mappings and import type when attempting an import, and repopulate these values when returning to the page.
* Update debugbar to fix a bug in the debugbar code.
* Fix asset tag detection.
Also rename findMatch to be a bit clearer as to what it does.
Remove logging to file of imports for http imports because
it eats an incredible amouint of memory.
This commit also moves imports out of the hardware namespace and into
their own webcontroller and route prefix, remove dead code from
AssetController as a result.
* Dynamically limit options for select2 based on import type selected, and group them by item type.
* Add user importer.
Still need to implement emailing of passwords to new users, and probably
test a bit more.
This also bumps the memory limit for web imports up as well, I need to
profile memory usage here before too long.
* Query the db to find user matches rather than search the array. Performance is much much better.
* Speed/memory improvements in importers.
Move to querying the db rather than maintaining an array for all
importers. Also only store the id of items when we import, rather than
the full model. It saves a decent amount of memory.
* Remove grouping of items in select2
With the values being set dynamically, the grouping is redundant. It
also caused a regression with automatically guessing/matching field
names. This is starting to get close.
* Remove debug line on every create.
* Switch migration to be text field instead of json field for compatibility with older mysql/mariadb
* Fix asset import regression matching email address.
* Rearrange travis order in attempt to fix null settings.
* Use auth::id instead of fetching it off the user. Fixes a null object reference during seeding.
2017-06-21 16:37:37 -07:00
'employee_num' ,
2018-02-24 19:01:34 -08:00
'first_name' ,
Importer mapping - v1 (#3677)
* Move importer to an inline-template, allows for translations and easier passing of data from laravel to vue.
* Pull the modal out into a dedicated partial, move importer to views/importer.
* Add document of CSV->importer mappings. Reorganize some code.
Progress.
* Add header_row and first_row to imports table, and process upon uploading a file
* Use an expandable table row instead of a modal for import processing. This should allow for field mapping interaction easier.
* Fix import processing after moving method.
* Frontend importer mapping improvements.
Invert display so we show found columns and allow users to select an
importer field to map to. Also implement sample data based on first row
of csv.
* Update select2. Maintain selected items properly.
* Backend support for importing. Only works on the web importer currently. Definitely needs testing and polish.
* We no longer use vue-modal plugin.
* Add a column to track field mappings to the imports table.
* Cleanup/rename methods+refactor
* Save field mappings and import type when attempting an import, and repopulate these values when returning to the page.
* Update debugbar to fix a bug in the debugbar code.
* Fix asset tag detection.
Also rename findMatch to be a bit clearer as to what it does.
Remove logging to file of imports for http imports because
it eats an incredible amouint of memory.
This commit also moves imports out of the hardware namespace and into
their own webcontroller and route prefix, remove dead code from
AssetController as a result.
* Dynamically limit options for select2 based on import type selected, and group them by item type.
* Add user importer.
Still need to implement emailing of passwords to new users, and probably
test a bit more.
This also bumps the memory limit for web imports up as well, I need to
profile memory usage here before too long.
* Query the db to find user matches rather than search the array. Performance is much much better.
* Speed/memory improvements in importers.
Move to querying the db rather than maintaining an array for all
importers. Also only store the id of items when we import, rather than
the full model. It saves a decent amount of memory.
* Remove grouping of items in select2
With the values being set dynamically, the grouping is redundant. It
also caused a regression with automatically guessing/matching field
names. This is starting to get close.
* Remove debug line on every create.
* Switch migration to be text field instead of json field for compatibility with older mysql/mariadb
* Fix asset import regression matching email address.
* Rearrange travis order in attempt to fix null settings.
* Use auth::id instead of fetching it off the user. Fixes a null object reference during seeding.
2017-06-21 16:37:37 -07:00
'jobtitle' ,
2018-02-24 19:01:34 -08:00
'last_name' ,
2018-04-20 14:02:52 -07:00
'ldap_import' ,
2018-02-24 19:01:34 -08:00
'locale' ,
Importer mapping - v1 (#3677)
* Move importer to an inline-template, allows for translations and easier passing of data from laravel to vue.
* Pull the modal out into a dedicated partial, move importer to views/importer.
* Add document of CSV->importer mappings. Reorganize some code.
Progress.
* Add header_row and first_row to imports table, and process upon uploading a file
* Use an expandable table row instead of a modal for import processing. This should allow for field mapping interaction easier.
* Fix import processing after moving method.
* Frontend importer mapping improvements.
Invert display so we show found columns and allow users to select an
importer field to map to. Also implement sample data based on first row
of csv.
* Update select2. Maintain selected items properly.
* Backend support for importing. Only works on the web importer currently. Definitely needs testing and polish.
* We no longer use vue-modal plugin.
* Add a column to track field mappings to the imports table.
* Cleanup/rename methods+refactor
* Save field mappings and import type when attempting an import, and repopulate these values when returning to the page.
* Update debugbar to fix a bug in the debugbar code.
* Fix asset tag detection.
Also rename findMatch to be a bit clearer as to what it does.
Remove logging to file of imports for http imports because
it eats an incredible amouint of memory.
This commit also moves imports out of the hardware namespace and into
their own webcontroller and route prefix, remove dead code from
AssetController as a result.
* Dynamically limit options for select2 based on import type selected, and group them by item type.
* Add user importer.
Still need to implement emailing of passwords to new users, and probably
test a bit more.
This also bumps the memory limit for web imports up as well, I need to
profile memory usage here before too long.
* Query the db to find user matches rather than search the array. Performance is much much better.
* Speed/memory improvements in importers.
Move to querying the db rather than maintaining an array for all
importers. Also only store the id of items when we import, rather than
the full model. It saves a decent amount of memory.
* Remove grouping of items in select2
With the values being set dynamically, the grouping is redundant. It
also caused a regression with automatically guessing/matching field
names. This is starting to get close.
* Remove debug line on every create.
* Switch migration to be text field instead of json field for compatibility with older mysql/mariadb
* Fix asset import regression matching email address.
* Rearrange travis order in attempt to fix null settings.
* Use auth::id instead of fetching it off the user. Fixes a null object reference during seeding.
2017-06-21 16:37:37 -07:00
'location_id' ,
2018-02-24 19:01:34 -08:00
'manager_id' ,
Importer mapping - v1 (#3677)
* Move importer to an inline-template, allows for translations and easier passing of data from laravel to vue.
* Pull the modal out into a dedicated partial, move importer to views/importer.
* Add document of CSV->importer mappings. Reorganize some code.
Progress.
* Add header_row and first_row to imports table, and process upon uploading a file
* Use an expandable table row instead of a modal for import processing. This should allow for field mapping interaction easier.
* Fix import processing after moving method.
* Frontend importer mapping improvements.
Invert display so we show found columns and allow users to select an
importer field to map to. Also implement sample data based on first row
of csv.
* Update select2. Maintain selected items properly.
* Backend support for importing. Only works on the web importer currently. Definitely needs testing and polish.
* We no longer use vue-modal plugin.
* Add a column to track field mappings to the imports table.
* Cleanup/rename methods+refactor
* Save field mappings and import type when attempting an import, and repopulate these values when returning to the page.
* Update debugbar to fix a bug in the debugbar code.
* Fix asset tag detection.
Also rename findMatch to be a bit clearer as to what it does.
Remove logging to file of imports for http imports because
it eats an incredible amouint of memory.
This commit also moves imports out of the hardware namespace and into
their own webcontroller and route prefix, remove dead code from
AssetController as a result.
* Dynamically limit options for select2 based on import type selected, and group them by item type.
* Add user importer.
Still need to implement emailing of passwords to new users, and probably
test a bit more.
This also bumps the memory limit for web imports up as well, I need to
profile memory usage here before too long.
* Query the db to find user matches rather than search the array. Performance is much much better.
* Speed/memory improvements in importers.
Move to querying the db rather than maintaining an array for all
importers. Also only store the id of items when we import, rather than
the full model. It saves a decent amount of memory.
* Remove grouping of items in select2
With the values being set dynamically, the grouping is redundant. It
also caused a regression with automatically guessing/matching field
names. This is starting to get close.
* Remove debug line on every create.
* Switch migration to be text field instead of json field for compatibility with older mysql/mariadb
* Fix asset import regression matching email address.
* Rearrange travis order in attempt to fix null settings.
* Use auth::id instead of fetching it off the user. Fixes a null object reference during seeding.
2017-06-21 16:37:37 -07:00
'password' ,
2018-01-17 05:33:55 -08:00
'phone' ,
2017-10-30 18:57:00 -07:00
'state' ,
2018-02-24 19:01:34 -08:00
'username' ,
2017-10-30 18:57:00 -07:00
'zip' ,
Importer mapping - v1 (#3677)
* Move importer to an inline-template, allows for translations and easier passing of data from laravel to vue.
* Pull the modal out into a dedicated partial, move importer to views/importer.
* Add document of CSV->importer mappings. Reorganize some code.
Progress.
* Add header_row and first_row to imports table, and process upon uploading a file
* Use an expandable table row instead of a modal for import processing. This should allow for field mapping interaction easier.
* Fix import processing after moving method.
* Frontend importer mapping improvements.
Invert display so we show found columns and allow users to select an
importer field to map to. Also implement sample data based on first row
of csv.
* Update select2. Maintain selected items properly.
* Backend support for importing. Only works on the web importer currently. Definitely needs testing and polish.
* We no longer use vue-modal plugin.
* Add a column to track field mappings to the imports table.
* Cleanup/rename methods+refactor
* Save field mappings and import type when attempting an import, and repopulate these values when returning to the page.
* Update debugbar to fix a bug in the debugbar code.
* Fix asset tag detection.
Also rename findMatch to be a bit clearer as to what it does.
Remove logging to file of imports for http imports because
it eats an incredible amouint of memory.
This commit also moves imports out of the hardware namespace and into
their own webcontroller and route prefix, remove dead code from
AssetController as a result.
* Dynamically limit options for select2 based on import type selected, and group them by item type.
* Add user importer.
Still need to implement emailing of passwords to new users, and probably
test a bit more.
This also bumps the memory limit for web imports up as well, I need to
profile memory usage here before too long.
* Query the db to find user matches rather than search the array. Performance is much much better.
* Speed/memory improvements in importers.
Move to querying the db rather than maintaining an array for all
importers. Also only store the id of items when we import, rather than
the full model. It saves a decent amount of memory.
* Remove grouping of items in select2
With the values being set dynamically, the grouping is redundant. It
also caused a regression with automatically guessing/matching field
names. This is starting to get close.
* Remove debug line on every create.
* Switch migration to be text field instead of json field for compatibility with older mysql/mariadb
* Fix asset import regression matching email address.
* Rearrange travis order in attempt to fix null settings.
* Use auth::id instead of fetching it off the user. Fixes a null object reference during seeding.
2017-06-21 16:37:37 -07:00
];
2016-03-25 01:18:05 -07:00
2016-12-19 11:04:28 -08:00
protected $casts = [
'activated' => 'boolean' ,
];
2016-03-25 01:18:05 -07:00
2016-06-02 17:16:22 -07:00
/**
* Model validation rules
*
* @ var array
*/
2016-03-25 01:18:05 -07:00
protected $rules = [
2016-06-02 17:16:22 -07:00
'first_name' => 'required|string|min:1' ,
2017-05-16 12:26:38 -07:00
'username' => 'required|string|min:1|unique_undeleted' ,
2017-08-22 20:32:39 -07:00
'email' => 'email|nullable' ,
2016-06-02 17:16:22 -07:00
'password' => 'required|min:6' ,
2017-10-28 11:17:52 -07:00
'locale' => 'max:10|nullable' ,
2016-03-25 01:18:05 -07:00
];
public function hasAccess ( $section )
{
2016-05-14 15:05:35 -07:00
if ( $this -> isSuperUser ()) {
return true ;
}
2016-06-02 02:49:32 -07:00
$user_groups = $this -> groups ;
2016-05-18 14:38:17 -07:00
2016-06-02 02:49:32 -07:00
if (( $this -> permissions == '' ) && ( count ( $user_groups ) == 0 )) {
2016-05-18 14:38:17 -07:00
return false ;
}
2016-06-02 02:49:32 -07:00
2016-03-25 01:18:05 -07:00
$user_permissions = json_decode ( $this -> permissions , true );
2016-07-28 20:59:42 -07:00
//If the user is explicitly granted, return true
2016-06-22 12:27:41 -07:00
if (( $user_permissions != '' ) && (( array_key_exists ( $section , $user_permissions )) && ( $user_permissions [ $section ] == '1' ))) {
2016-06-15 20:45:45 -07:00
return true ;
}
// If the user is explicitly denied, return false
2016-06-22 12:27:41 -07:00
if (( $user_permissions == '' ) || array_key_exists ( $section , $user_permissions ) && ( $user_permissions [ $section ] == '-1' )) {
2016-06-15 20:45:45 -07:00
return false ;
2016-03-25 01:18:05 -07:00
}
2016-06-15 20:45:45 -07:00
// Loop through the groups to see if any of them grant this permission
2016-03-25 01:18:05 -07:00
foreach ( $user_groups as $user_group ) {
2016-08-30 13:25:14 -07:00
$group_permissions = ( array ) json_decode ( $user_group -> permissions , true );
2016-06-02 02:49:32 -07:00
if ((( array_key_exists ( $section , $group_permissions )) && ( $group_permissions [ $section ] == '1' ))) {
2016-06-15 20:45:45 -07:00
return true ;
2016-03-25 01:18:05 -07:00
}
}
2016-06-15 20:45:45 -07:00
return false ;
2016-03-25 01:18:05 -07:00
}
2016-06-22 12:27:41 -07:00
public function isSuperUser ()
{
2016-05-12 21:01:31 -07:00
if ( ! $user_permissions = json_decode ( $this -> permissions , true )) {
return false ;
}
2016-05-14 15:05:35 -07:00
2016-06-02 02:49:32 -07:00
foreach ( $this -> groups as $user_group ) {
2016-05-12 21:01:31 -07:00
$group_permissions = json_decode ( $user_group -> permissions , true );
2016-08-30 13:25:14 -07:00
$group_array = ( array ) $group_permissions ;
2016-06-02 02:49:32 -07:00
if (( array_key_exists ( 'superuser' , $group_array )) && ( $group_permissions [ 'superuser' ] == '1' )) {
return true ;
}
2016-05-12 21:01:31 -07:00
}
2016-06-02 17:16:22 -07:00
2016-03-25 01:18:05 -07:00
if (( array_key_exists ( 'superuser' , $user_permissions )) && ( $user_permissions [ 'superuser' ] == '1' )) {
return true ;
}
2016-06-02 02:49:32 -07:00
return false ;
2016-03-25 01:18:05 -07:00
}
public function company ()
{
return $this -> belongsTo ( '\App\Models\Company' , 'company_id' );
}
2017-05-23 02:49:27 -07:00
public function department ()
{
return $this -> belongsTo ( '\App\Models\Department' , 'department_id' );
}
2016-03-25 01:18:05 -07:00
public function isActivated ()
{
2016-12-23 17:52:00 -08:00
return $this -> activated == 1 ;
2016-03-25 01:18:05 -07:00
}
2016-05-17 21:31:57 -07:00
public function getFullNameAttribute ()
{
2016-06-02 17:16:22 -07:00
return $this -> first_name . " " . $this -> last_name ;
2016-05-17 21:31:57 -07:00
}
2016-03-25 01:18:05 -07:00
2016-06-01 11:43:43 -07:00
public function getCompleteNameAttribute ()
{
2016-06-02 17:16:22 -07:00
return $this -> last_name . " , " . $this -> first_name . " ( " . $this -> username . " ) " ;
2016-06-01 11:43:43 -07:00
}
2018-03-25 13:46:57 -07:00
/**
* The url for slack notifications .
* Used by Notifiable trait .
* @ return mixed
*/
public function routeNotificationForSlack ()
{
// At this point the endpoint is the same for everything.
// In the future this may want to be adapted for individual notifications.
$this -> endpoint = \App\Models\Setting :: getSettings () -> slack_endpoint ;
return $this -> endpoint ;
}
2016-03-25 01:18:05 -07:00
2016-06-02 17:16:22 -07:00
/**
* Get assets assigned to this user
*/
2017-02-03 22:20:11 -08:00
public function assets ()
2016-03-25 01:18:05 -07:00
{
2016-12-27 16:24:41 -08:00
return $this -> morphMany ( 'App\Models\Asset' , 'assigned' , 'assigned_type' , 'assigned_to' ) -> withTrashed ();
// return $this->hasMany('\App\Models\Asset', 'assigned_to')->withTrashed();
2016-03-25 01:18:05 -07:00
}
2016-06-22 17:04:47 -07:00
/**
* Get assets assigned to this user
*/
public function assetmaintenances ()
{
return $this -> hasMany ( '\App\Models\AssetMaintenance' , 'user_id' ) -> withTrashed ();
}
2016-06-02 17:16:22 -07:00
/**
* Get accessories assigned to this user
*/
2016-03-25 01:18:05 -07:00
public function accessories ()
{
return $this -> belongsToMany ( '\App\Models\Accessory' , 'accessories_users' , 'assigned_to' , 'accessory_id' ) -> withPivot ( 'id' ) -> withTrashed ();
}
2016-06-02 17:16:22 -07:00
/**
* Get consumables assigned to this user
*/
2016-03-25 01:18:05 -07:00
public function consumables ()
{
return $this -> belongsToMany ( '\App\Models\Consumable' , 'consumables_users' , 'assigned_to' , 'consumable_id' ) -> withPivot ( 'id' ) -> withTrashed ();
}
2016-06-02 17:16:22 -07:00
/**
* Get licenses assigned to this user
*/
2016-03-25 01:18:05 -07:00
public function licenses ()
{
return $this -> belongsToMany ( '\App\Models\License' , 'license_seats' , 'assigned_to' , 'license_id' ) -> withPivot ( 'id' );
}
2016-06-02 17:16:22 -07:00
/**
* Get action logs for this user
*/
2016-03-25 01:18:05 -07:00
public function userlog ()
{
2016-09-06 19:39:42 -07:00
return $this -> hasMany ( '\App\Models\Actionlog' , 'target_id' ) -> orderBy ( 'created_at' , 'DESC' ) -> withTrashed ();
2016-03-25 01:18:05 -07:00
}
2016-06-02 17:16:22 -07:00
/**
* Get the asset ' s location based on the assigned user
2017-10-31 05:22:57 -07:00
* @ todo - this should be removed once we 're sure we' ve switched it
* to location ()
2016-06-02 17:16:22 -07:00
**/
2016-03-25 01:18:05 -07:00
public function userloc ()
{
return $this -> belongsTo ( '\App\Models\Location' , 'location_id' ) -> withTrashed ();
}
2017-10-31 05:22:57 -07:00
/**
* Get the asset ' s location based on the assigned user
**/
public function location ()
{
return $this -> belongsTo ( '\App\Models\Location' , 'location_id' ) -> withTrashed ();
}
2016-06-02 17:16:22 -07:00
/**
* Get the user ' s manager based on the assigned user
**/
2016-03-25 01:18:05 -07:00
public function manager ()
{
return $this -> belongsTo ( '\App\Models\User' , 'manager_id' ) -> withTrashed ();
}
2017-05-22 17:27:00 -07:00
/**
* Get any locations the user manages .
**/
public function managedLocations ()
{
return $this -> hasMany ( '\App\Models\Location' , 'manager_id' ) -> withTrashed ();
}
2016-06-02 17:16:22 -07:00
/**
* Get user groups
*/
2016-03-25 01:18:05 -07:00
public function groups ()
{
2016-06-15 20:45:45 -07:00
return $this -> belongsToMany ( '\App\Models\Group' , 'users_groups' );
2016-03-25 01:18:05 -07:00
}
public function accountStatus ()
{
2016-08-02 01:23:53 -07:00
if ( $this -> throttle ) {
if ( $this -> throttle -> suspended == 1 ) {
2016-03-25 01:18:05 -07:00
return 'suspended' ;
2016-08-02 01:23:53 -07:00
} elseif ( $this -> throttle -> banned == 1 ) {
2016-03-25 01:18:05 -07:00
return 'banned' ;
} else {
return false ;
}
} else {
return false ;
}
}
public function assetlog ()
{
return $this -> hasMany ( '\App\Models\Asset' , 'id' ) -> withTrashed ();
}
2016-06-02 17:16:22 -07:00
/**
* Get uploads for this asset
*/
2016-03-25 01:18:05 -07:00
public function uploads ()
{
2016-09-06 19:39:42 -07:00
return $this -> hasMany ( '\App\Models\Actionlog' , 'item_id' )
-> where ( 'item_type' , User :: class )
2016-06-02 17:16:22 -07:00
-> where ( 'action_type' , '=' , 'uploaded' )
-> whereNotNull ( 'filename' )
-> orderBy ( 'created_at' , 'desc' );
2016-03-25 01:18:05 -07:00
}
2016-09-15 19:58:27 -07:00
/**
* Fetch Items User has requested
*/
public function checkoutRequests ()
{
2018-04-04 17:33:02 -07:00
return $this -> belongsToMany ( Asset :: class , 'checkout_requests' , 'user_id' , 'requestable_id' ) -> whereNull ( 'canceled_at' );
2016-09-15 19:58:27 -07:00
}
2016-08-02 01:23:53 -07:00
public function throttle ()
2016-03-25 01:18:05 -07:00
{
return $this -> hasOne ( '\App\Models\Throttle' );
}
public function scopeGetDeleted ( $query )
{
return $query -> withTrashed () -> whereNotNull ( 'deleted_at' );
}
public function scopeGetNotDeleted ( $query )
{
return $query -> whereNull ( 'deleted_at' );
}
2016-06-02 17:16:22 -07:00
/**
* Override the SentryUser getPersistCode method for
* multiple logins at one time
**/
2016-03-25 01:18:05 -07:00
public function getPersistCode ()
{
if ( ! config ( 'session.multi_login' ) || ( ! $this -> persist_code )) {
$this -> persist_code = $this -> getRandomString ();
// Our code got hashed
$persistCode = $this -> persist_code ;
$this -> save ();
return $persistCode ;
}
return $this -> persist_code ;
}
public function scopeMatchEmailOrUsername ( $query , $user_username , $user_email )
{
return $query -> where ( 'email' , '=' , $user_email )
2016-06-02 17:16:22 -07:00
-> orWhere ( 'username' , '=' , $user_username )
-> orWhere ( 'username' , '=' , $user_email );
2016-03-25 01:18:05 -07:00
}
2016-12-29 14:02:18 -08:00
public static function generateEmailFromFullName ( $name )
{
2016-08-12 16:02:39 -07:00
$username = User :: generateFormattedNameFromFullName ( Setting :: getSettings () -> email_format , $name );
return $username [ 'username' ] . '@' . Setting :: getSettings () -> email_domain ;
}
2016-03-25 01:18:05 -07:00
public static function generateFormattedNameFromFullName ( $format = 'filastname' , $users_name )
{
2018-02-22 14:10:58 -08:00
// If there was only one name given
if ( strpos ( $users_name , ' ' ) === false ) {
$first_name = $users_name ;
$last_name = '' ;
$username = $users_name ;
2016-08-12 16:02:39 -07:00
2018-02-22 14:10:58 -08:00
} else {
list ( $first_name , $last_name ) = explode ( " " , $users_name , 2 );
// Assume filastname by default
$username = str_slug ( substr ( $first_name , 0 , 1 ) . $last_name );
2016-08-12 16:02:39 -07:00
2018-02-22 14:10:58 -08:00
if ( $format == 'firstname.lastname' ) {
2018-03-05 21:39:05 -08:00
$username = str_slug ( $first_name ) . '.' . str_slug ( $last_name );
} elseif ( $format == 'lastnamefirstinitial' ) {
$username = str_slug ( $last_name . substr ( $first_name , 0 , 1 ));
2018-02-22 14:10:58 -08:00
} elseif ( $format == 'firstname_lastname' ) {
$username = str_slug ( $first_name ) . '_' . str_slug ( $last_name );
} elseif ( $format == 'firstname' ) {
$username = str_slug ( $first_name );
}
2016-03-25 01:18:05 -07:00
}
$user [ 'first_name' ] = $first_name ;
$user [ 'last_name' ] = $last_name ;
2018-02-13 20:31:11 -08:00
$user [ 'username' ] = strtolower ( $username );
2016-03-25 01:18:05 -07:00
return $user ;
}
2017-07-29 16:25:42 -07:00
/**
* Check whether two - factor authorization is required and the user has activated it
*
* @ author [ A . Gianotto ] [ < snipe @ snipe . net > ]
* @ since [ v4 . 0 ]
*
* @ return bool
*/
public function two_factor_active () {
if ( Setting :: getSettings () -> two_factor_enabled != '0' ) {
if (( $this -> two_factor_optin == '1' ) && ( $this -> two_factor_enrolled )) {
return true ;
}
}
return false ;
}
2016-04-28 21:59:43 -07:00
public function decodePermissions ()
{
return json_decode ( $this -> permissions , true );
}
2017-10-17 21:43:57 -07:00
public function scopeByGroup ( $query , $id ) {
return $query -> whereHas ( 'groups' , function ( $query ) use ( $id ) {
2017-10-24 04:39:47 -07:00
$query -> where ( 'groups.id' , '=' , $id );
2017-10-17 21:43:57 -07:00
});
}
2016-06-02 17:16:22 -07:00
/**
* Query builder scope to search on text
*
* @ param Illuminate\Database\Query\Builder $query Query builder instance
* @ param text $search Search term
*
* @ return Illuminate\Database\Query\Builder Modified query builder
*/
2016-03-25 01:18:05 -07:00
public function scopeTextsearch ( $query , $search )
{
return $query -> where ( function ( $query ) use ( $search ) {
$query -> where ( 'users.first_name' , 'LIKE' , " % $search % " )
2016-06-02 17:16:22 -07:00
-> orWhere ( 'users.last_name' , 'LIKE' , " % $search % " )
-> orWhere ( 'users.email' , 'LIKE' , " % $search % " )
-> orWhere ( 'users.username' , 'LIKE' , " % $search % " )
-> orWhere ( 'users.notes' , 'LIKE' , " % $search % " )
2017-10-23 14:21:51 -07:00
-> orWhere ( 'users.phone' , 'LIKE' , " % $search % " )
2016-10-27 14:29:07 -07:00
-> orWhere ( 'users.jobtitle' , 'LIKE' , " % $search % " )
2016-06-02 17:16:22 -07:00
-> orWhere ( 'users.employee_num' , 'LIKE' , " % $search % " )
2017-12-12 12:52:10 -08:00
-> orWhereRaw ( 'CONCAT(' . DB :: getTablePrefix () . 'users.first_name," ",' . DB :: getTablePrefix () . 'users.last_name) LIKE ?' , [ " % $search % " , " % $search % " ])
2016-06-02 17:16:22 -07:00
-> orWhere ( function ( $query ) use ( $search ) {
$query -> whereHas ( 'userloc' , function ( $query ) use ( $search ) {
$query -> where ( 'locations.name' , 'LIKE' , '%' . $search . '%' );
});
})
2017-05-23 02:49:27 -07:00
-> orWhere ( function ( $query ) use ( $search ) {
$query -> whereHas ( 'department' , function ( $query ) use ( $search ) {
$query -> where ( 'departments.name' , 'LIKE' , '%' . $search . '%' );
});
})
2016-10-31 18:27:34 -07:00
-> orWhere ( function ( $query ) use ( $search ) {
$query -> whereHas ( 'groups' , function ( $query ) use ( $search ) {
$query -> where ( 'groups.name' , 'LIKE' , '%' . $search . '%' );
});
})
2016-06-02 17:16:22 -07:00
2017-11-03 14:47:31 -07:00
//Ugly, ugly code because Laravel sucks at self-joins
2016-06-02 17:16:22 -07:00
-> orWhere ( function ( $query ) use ( $search ) {
2017-12-11 22:31:07 -08:00
$query -> whereRaw ( DB :: getTablePrefix () . " users.manager_id IN (select id from " . DB :: getTablePrefix () . " users where first_name LIKE ? OR last_name LIKE ?) " , [ " % $search % " , " % $search % " ]);
2016-03-25 01:18:05 -07:00
});
2017-11-03 14:47:31 -07:00
2016-03-25 01:18:05 -07:00
});
}
2016-06-02 17:16:22 -07:00
/**
* Query builder scope for Deleted users
*
* @ param Illuminate\Database\Query\Builder $query Query builder instance
*
* @ return Illuminate\Database\Query\Builder Modified query builder
*/
2016-03-25 01:18:05 -07:00
public function scopeDeleted ( $query )
{
return $query -> whereNotNull ( 'deleted_at' );
}
2016-06-02 17:16:22 -07:00
/**
* Query builder scope to order on manager
*
* @ param Illuminate\Database\Query\Builder $query Query builder instance
* @ param text $order Order
*
* @ return Illuminate\Database\Query\Builder Modified query builder
*/
2016-03-25 01:18:05 -07:00
public function scopeOrderManager ( $query , $order )
{
2016-06-02 17:16:22 -07:00
// Left join here, or it will only return results with parents
2017-11-03 14:47:31 -07:00
return $query -> leftJoin ( 'users as users_manager' , 'users.manager_id' , '=' , 'users_manager.id' ) -> orderBy ( 'users_manager.first_name' , $order ) -> orderBy ( 'users_manager.last_name' , $order );
2016-03-25 01:18:05 -07:00
}
2016-06-02 17:16:22 -07:00
/**
* Query builder scope to order on company
*
* @ param Illuminate\Database\Query\Builder $query Query builder instance
* @ param text $order Order
*
* @ return Illuminate\Database\Query\Builder Modified query builder
*/
2016-03-25 01:18:05 -07:00
public function scopeOrderLocation ( $query , $order )
{
2017-11-03 14:47:31 -07:00
return $query -> leftJoin ( 'locations as locations_users' , 'users.location_id' , '=' , 'locations_users.id' ) -> orderBy ( 'locations_users.name' , $order );
2016-03-25 01:18:05 -07:00
}
2017-05-23 02:49:27 -07:00
/**
* Query builder scope to order on department
*
* @ param Illuminate\Database\Query\Builder $query Query builder instance
* @ param text $order Order
*
* @ return Illuminate\Database\Query\Builder Modified query builder
*/
public function scopeOrderDepartment ( $query , $order )
{
2017-11-03 14:47:31 -07:00
return $query -> leftJoin ( 'departments as departments_users' , 'users.department_id' , '=' , 'departments_users.id' ) -> orderBy ( 'departments_users.name' , $order );
2017-05-23 02:49:27 -07:00
}
2016-03-25 01:18:05 -07:00
}