daily pastebin goal
78%
SHARE
TWEET

Untitled

a guest Mar 20th, 2017 55 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /** Code Example for following Poits
  2. * Candidate must have hands on experience with Laravel Framework.
  3. * Candidate must have expertise in database relations and sql.  
  4. /
  5. <?php
  6.  
  7. namespace App;
  8.  
  9. use Illuminate\Foundation\Auth\User as Authenticatable;
  10. use Carbon\Carbon;
  11. use App\Interfaces\SaveObjectsInterface;
  12. use Illuminate\Support\Collection;
  13. use Illuminate\Support\Facades\Auth;
  14. use App\Services\CommonService;
  15. use App\Roles;
  16.  
  17. class User extends Authenticatable implements SaveObjectsInterface
  18. {
  19.     /**
  20.      *The attributes that is used to define table name for this model.
  21.      */
  22.     protected $table = "users";
  23.  
  24.     /**
  25.      * The attributes that are mass assignable.
  26.      * @var array
  27.      */
  28.     protected $fillable = [
  29.         'first_name', 'last_name', 'address', 'city', 'state',
  30.         'zip', 'payment_address', 'payment_city', 'payment_state', 'payment_zip', 'home_phone', 'cell_phone', 'work_phone', 'fax_phone', 'email',
  31.         'email2', 'password', 'password_expire_date', 'created_by', 'last_login', 'permanent', 'status'
  32.     ];
  33.  
  34.     /**
  35.      * The attributes excluded from the model's JSON form.
  36.      * @var array
  37.      */
  38.     protected $hidden = [
  39.         'password', 'remember_token',
  40.     ];
  41.  
  42.     /**
  43.      * This method is used to format the created_at date.
  44.      * @param $value
  45.      * @return string
  46.      */
  47.     protected function getCreatedAtAttribute($value)
  48.     {
  49.         return CommonService::getDate($value, env('DATE_FORMAT'));
  50.     }
  51.  
  52.     /**
  53.      * This method is used to format the password_expire_date date.
  54.      * @param $value
  55.      * @return string
  56.      */
  57.     protected function getPasswordExpireDateAttribute($value)
  58.     {
  59.         return CommonService::getDate($value, env('DATE_FORMAT'));
  60.     }
  61.  
  62.     /**
  63.      * This method is used to format the last_login date.
  64.      * @param $value
  65.      * @return string
  66.      */
  67.     protected function getLastLoginAttribute($value)
  68.     {
  69.         return CommonService::getDate($value, env('DATE_FORMAT'));
  70.     }
  71.  
  72.     /**
  73.      * This method is set the password_expire_date format to save in database
  74.      * @param $value
  75.      */
  76.     protected function setPasswordExpireDateAttribute($value)
  77.     {
  78.         $this->attributes['password_expire_date'] = CommonService::setDate($value, "Y-m-d H:i:s");
  79.     }
  80.  
  81.     /**
  82.      * The roles that belongs to user
  83.      * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
  84.      */
  85.     public function roles()
  86.     {
  87.         return $this->belongsToMany('App\Roles', 'user_roles', 'user_id', 'role_id');
  88.     }
  89.  
  90.     /**
  91.      * The companies that belongs to user
  92.      * @return $this
  93.      */
  94.     public function companies()
  95.     {
  96.         return $this->belongsToMany('App\Company', 'user_companies', 'user_id', 'company_id')->withPivot('percent_owned');
  97.     }
  98.  
  99.     /**
  100.      * The user that created company
  101.      * @return \Illuminate\Database\Eloquent\Relations\HasMany
  102.      */
  103.     public function companyCreatedByUser()
  104.     {
  105.         return $this->hasMany('App\Company', 'created_by');
  106.     }
  107.  
  108.     /**
  109.      * The reports that belongs to user
  110.      * @return \Illuminate\Database\Eloquent\Relations\HasMany
  111.      */
  112.     public function reports()
  113.     {
  114.         return $this->hasMany('App\UserReports', 'user_id');
  115.     }
  116.  
  117.     /**
  118.      * The user that created property reports
  119.      * @return \Illuminate\Database\Eloquent\Relations\HasMany
  120.      */
  121.     public function propertyReportsCreatedByUser()
  122.     {
  123.         return $this->hasMany('App\PropertyReports', 'created_by');
  124.     }
  125.  
  126.     /**
  127.      * The user that created the user
  128.      * @return \Illuminate\Database\Eloquent\Relations\HasMany
  129.      */
  130.     public function createdByUser()
  131.     {
  132.         return $this->hasMany('App\User', 'id', 'created_by');
  133.     }
  134.  
  135.     /**
  136.      * Permissions of Users
  137.      * @param $permission
  138.      * @return mixed
  139.      */
  140.     public function hasPermission($permission)
  141.     {
  142.         foreach ($this->roles()->get() as $role) {
  143.             $result = Roles::where('name', '=', $role->name)->first()->hasPermissionRole($permission);
  144.             return $result;
  145.         }
  146.     }
  147.  
  148.     /**
  149.      * @param $roleName
  150.      * @return bool
  151.      */
  152.     public function hasRole($roleName)
  153.     {
  154.         foreach ($this->roles()->get() as $role) {
  155.             if ($role->name == $roleName) {
  156.                 return true;
  157.             } else {
  158.                 return false;
  159.             }
  160.         }
  161.     }
  162.  
  163.  
  164.     /**
  165.      * This method is used to set values for User model object
  166.      * @param $modelObject
  167.      * @param $values
  168.      * @param int $update
  169.      * @return mixed
  170.      */
  171.     public function setObjectValues($modelObject, $values, $update = 0)
  172.     {
  173.         $modelObject['first_name']              = $values['first_name'];
  174.         $modelObject['last_name']               = $values['last_name'];
  175.         $modelObject['address']                 = $values['address'];
  176.         $modelObject['city']                    = $values['city'];
  177.         $modelObject['state']                   = $values['state'];
  178.         $modelObject['zip']                     = $values['zip'];
  179.         $modelObject['payment_address']         = $values['payment_address'];
  180.         $modelObject['payment_city']            = $values['payment_city'];
  181.         $modelObject['payment_state']           = $values['payment_state'];
  182.         $modelObject['payment_zip']             = $values['payment_zip'];
  183.         $modelObject['home_phone']              = $values['home_phone'];
  184.         $modelObject['cell_phone']              = $values['cell_phone'];
  185.         $modelObject['work_phone']              = $values['work_phone'];
  186.         $modelObject['fax_phone']               = $values['fax_phone'];
  187.         $modelObject['email']                   = $values['email'];
  188.         $modelObject['email2']                  = $values['email2'];
  189.         if ($update == 0) {
  190.             $modelObject['password']            = bcrypt($values['password']);
  191.             $modelObject['created_by']          = Auth::user()->id;
  192.         }
  193.         $modelObject['password_expire_date']    = $values['password_expire_date'];
  194.         $modelObject['permanent']               = (int)$values['permanent'];
  195.         $modelObject['status']                  = $values['status'] != "" ? (int)$values['status'] : env('USER_STATUS');
  196.  
  197.         return $modelObject;
  198.     }
  199.  
  200.  
  201.     /**
  202.      * This method is used to save the User model's object
  203.      * @param $modelObject
  204.      * @return mixed
  205.      */
  206.     public function saveObject($modelObject)
  207.     {
  208.         $modelObject->save();
  209.         return $modelObject;
  210.     }
  211.  
  212.  
  213.     /**
  214.      * This method is used to save new record of User in database
  215.      * @param $request
  216.      * @return Collection
  217.      */
  218.     public function saveUserRecord($request)
  219.     {
  220.         $fileTypeResult = CommonService::checkFileType('userReport', config('app.allowedFileTypeArray'));
  221.         if ($fileTypeResult) {
  222.             $userObject = $this->setObjectValues(new User(), $request);
  223.             $saveUser = $this->saveObject($userObject);
  224.             $this->assignRole($saveUser, $request['role']);
  225.             $this->assignCompany($saveUser, json_decode($request['companyDataArray']));
  226.             if($request->hasFile('userReport')){
  227.                 $this->saveUserReports($saveUser, $request['reportTitle']);
  228.             }
  229.             $userResult = collect(['status' => 'success', 'userObject' => $saveUser]);
  230.         } else {
  231.             $userResult = collect(['status' => 'fail', 'message' => 'Invalid file extension']);
  232.         }
  233.         return $userResult;
  234.     }
  235.  
  236.  
  237.     /**
  238.      * This method is used to update existing record of User in database
  239.      * @param $request
  240.      * @return Collection
  241.      */
  242.     public function updateUserRecord($request)
  243.     {
  244.         $userResult = collect(['status' => 'fail', 'message' => 'Nothing to update']);
  245.         if (!empty($request['user_id'])) {
  246.             if ($request['user_id'] == Auth::user()->id && $request['status'] == 0) {
  247.                 $userResult = collect(['status' => 'fail', 'message' => 'User cannot deactivate himself', 'bit' => 2]);
  248.             } else {
  249.                 $fileTypeResult = CommonService::checkFileType('userReport', config('app.allowedFileTypeArray'));
  250.                 if ($fileTypeResult) {
  251.                     $userObject = $this->setObjectValues($this::find($request['user_id']), $request, 1);
  252.                     $saveUser = $this->saveObject($userObject);
  253.                     $this->removeRole($saveUser);
  254.                     $this->assignRole($saveUser, $request['role']);
  255.                     $this->removeCompany($saveUser);
  256.                     $this->assignCompany($saveUser, json_decode($request['companyDataArray']));
  257.                     if($request->hasFile('userReport')){
  258.                         $this->saveUserReports($saveUser, $request['reportTitle']);
  259.                     }
  260.                     $userResult = collect(['status' => 'success', 'userObject' => $saveUser]);
  261.                 } else {
  262.                     $userResult = collect(['status' => 'fail', 'message' => 'Invalid file extension']);
  263.                 }
  264.             }
  265.         }
  266.         return $userResult;
  267.     }
  268.  
  269.  
  270.     /**
  271.      * This method is used to login existing User
  272.      * @param $request
  273.      * @return Collection
  274.      */
  275.     public function loginUser($request)
  276.     {
  277.         $remember = $request['remember'] ? $request['remember'] : 0; /*use for remember me functionality*/
  278.         $email = $request['email'];
  279.         $password = $request['password'];
  280.         $active_status = env('USER_STATUS');
  281.         if (Auth::attempt(['email' => $email, 'password' => $password], $remember)) {
  282.             if (Auth::User()->status != $active_status) {
  283.                 Auth::logout();
  284.                 return collect(['status' => 'failure', 'message' => 'Your account is deactivated..!']);
  285.             }
  286.             if (!Auth::User()->permanent || Auth::User()->permanent < 1  && strtotime(Auth::User()->password_expire_date) < 0) {
  287.                 return collect(['status' => 'success', 'redirectTo' => '/home']); // logout the user if not permanent and the password is expired
  288.             }
  289.             if (Auth::User()->permanent != 1 && Auth::User()->password_expire_date < Carbon::now()) {
  290.                 Auth::logout();
  291.                 return collect(['status' => 'failure', 'message' => 'Your password is expired..!']);
  292.             }
  293.             return collect(['status' => 'success', 'redirectTo' => '/home']);
  294.         }
  295.         return collect(['status' => 'failure', 'message' => 'Invalid Credentials']);
  296.     }
  297.  
  298.  
  299.     /**
  300.      * This method is used to update the logout time of user
  301.      */
  302.     public function updateLogoutTime()
  303.     {
  304.         $user = Auth::User();
  305.         $user->last_login = Carbon::now();
  306.         $user->save();
  307.     }
  308.  
  309.     /**
  310.      * This method is used to logout the user
  311.      **/
  312.     public function logoutUser()
  313.     {
  314.         $this->updateLogoutTime();
  315.         Auth::logout();
  316.     }
  317.  
  318.     /**
  319.      * Assign a role to the user
  320.      * @param $role
  321.      * @return mixed
  322.      */
  323.     public function assignRole($userObject, $role)
  324.     {
  325.         $userObject->roles()->attach($role);
  326.         return $userObject;
  327.     }
  328.  
  329.     /**
  330.      * Remove all roles from a user
  331.      * @param $role
  332.      * @return mixed
  333.      */
  334.     public function removeRole($userObject)
  335.     {
  336.         $userObject->roles()->detach();
  337.         return $userObject;
  338.     }
  339.  
  340.     /**
  341.      * Assign a Company to the User
  342.      * @param $company
  343.      * @return mixed
  344.      */
  345.     public function assignCompany($userObject, $company)
  346.     {
  347.         foreach ($company as $companyData) {
  348.             $userObject->companies()->attach($companyData->id, ['percent_owned' => $companyData->percentOwned]);
  349.         }
  350.         return $userObject;
  351.     }
  352.  
  353.     /**
  354.      * Remove all Companies from a User
  355.      * @param $company
  356.      * @return mixed
  357.      */
  358.     public function removeCompany($userObject)
  359.     {
  360.         $userObject->companies()->detach();
  361.         return $userObject;
  362.     }
  363.  
  364.  
  365.     /**
  366.      * This method is used to save user reports
  367.      * @param $userObject
  368.      * @param $reportTitle
  369.      * @return mixed
  370.      */
  371.     public function saveUserReports($userObject, $reportTitle)
  372.     {
  373.         $reportName = CommonService::uploadFile('userReport', env('USER_REPORT_UPLOAD_PATH'));
  374.         $userObject->reports()->create([
  375.             'report' => $reportName,
  376.             'report_date' => Carbon::now(),
  377.             'report_title' => $reportTitle,
  378.             'created_by' => $userObject['created_by'],
  379.         ]);
  380.         return $userObject;
  381.     }
  382.  
  383.  
  384.     /**
  385.      * This method is used fetch users information along company and role
  386.      * @return \Illuminate\Database\Eloquent\Collection|static[]
  387.      */
  388.     public function fetchUsersAndRelatedData()
  389.     {
  390.         $usersAndRelatedData = $this::with(array(
  391.             'roles', 'companies', 'createdByUser', 'companies.properties' => function ($query) {
  392.                 $query->select('name');
  393.             }))->orderBy('updated_at', 'desc')->get();
  394.         return $usersAndRelatedData;
  395.     }
  396.  
  397.     /**
  398.      * This method is used reset user password
  399.      * @param $request
  400.      * @return Collection
  401.      */
  402.     public function resetPassword($request)
  403.     {
  404.         $userResult = collect(['status' => 'fail', 'message' => 'Something went wrong while reset password']);
  405.         if (!empty($request['user_id']) && !empty($request['reset_password'])) {
  406.             $user = $this::find($request['user_id']);
  407.             $user->password = bcrypt($request['reset_password']);
  408.             $saveUser = $this->saveObject($user);
  409.             $userResult = collect(['status' => 'success', 'message' => $saveUser]);
  410.         }
  411.         return $userResult;
  412.     }
  413.  
  414. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top