Commit d31c62e0 authored by Viral Solani's avatar Viral Solani

Refactor Create User

parent 70812313
......@@ -69,7 +69,7 @@ class UserController extends Controller
*/
public function store(StoreUserRequest $request)
{
$this->users->create(['data' => $request->except('assignees_roles'), 'roles' => $request->all('assignees_roles')]);
$this->users->create($request);
return redirect()->route('admin.access.user.index')->withFlashSuccess(trans('alerts.backend.users.created'));
}
......
......@@ -31,11 +31,7 @@ class StoreUserRequest extends Request
'first_name' => 'required|max:255',
'last_name' => 'required|max:255',
'email' => ['required', 'email', 'max:255', Rule::unique('users')],
'password' => 'required|min:6|confirmed',
'state_id' => 'required',
'city_id' => 'required',
'zip_code' => 'required|regex:/^[0-9]+$/',
'ssn' => 'required|regex:/^[0-9]+$/|max:9|min:9',
'password' => 'required|min:6|confirmed'
];
}
......@@ -47,12 +43,6 @@ class StoreUserRequest extends Request
public function messages()
{
return [
'state_id.required' => 'The state field is required.',
'city_id.required' => 'The city field is required.',
'ssn.regex' => 'The SSN field must be 9 digits.',
'ssn.min' => 'The SSN field must be 9 digits.',
'ssn.max' => 'The SSN field must be 9 digits.',
'zip_code.regex' => 'The zip code field must be digit.',
];
}
}
......@@ -9,7 +9,7 @@ namespace App\Http\Utilities;
*
* Notification class is a abstract class for send push notification in Android and iPhone mobile
*
* Author Sandip P. Joshi
* Author Viral Solani
*
* Version 1.0
*
......
......@@ -89,7 +89,6 @@ trait UserAccess
public function allow($nameOrId)
{
/*
* Author : Vaishal Gandhi
*
* Update for this function due to issue of user custom permission
*/
......@@ -117,7 +116,6 @@ trait UserAccess
}
/*
* Author : Vaishal Gandhi
*
* below code is commented due to issue of user custom permisssion
* if this code is not commented then if user dont have permission of one module but role which is assigned to that user have that permission than allow() method return true
......
......@@ -3,8 +3,8 @@
namespace App\Notifications\Frontend\Auth;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
use Illuminate\Notifications\Messages\MailMessage;
/**
* Class UserNeedsConfirmation.
......
......@@ -113,19 +113,19 @@ class UserRepository extends BaseRepository
}
/**
* @param Model $input
* Create User
*
* @param Model $request
*
*/
public function create($input)
public function create($request)
{
$data = $input['data'];
$roles = $input['roles'];
$permissions = isset($data['permissions']) ? $data['permissions'] : [];
unset($data['permissions']);
$user = $this->createUserStub($data);
$data = $request->except('assignees_roles','permissions');
$roles = $request->all('assignees_roles');
$permissions = $request->all('permissions') ? $request->all('permissions') : [];
$user = $this->createUserStub($data);
DB::transaction(function () use ($user, $data, $roles, $permissions, $input) {
DB::transaction(function () use ($user, $data, $roles, $permissions) {
// Set email type 2
$email_type = 2;
......@@ -139,26 +139,21 @@ class UserRepository extends BaseRepository
//Attach new roles
$user->attachRoles($roles['assignees_roles']);
//Send confirmation email if requested
if (isset($data['confirmation_email']) && $user->confirmed == 0) {
// If user needs confirmation then set email type 1
$email_type = 1;
$input['data']['confirmation_code'] = $user->confirmation_code;
// Attach Permissions
$this->attachPermissions($permissions);
//Send confirmation email if requested and account approval is off
if (isset($data['confirmation_email']) && $user->confirmed == 0 ) {
$user->notify(new UserNeedsConfirmation($user->confirmation_code));
}
event(new UserCreated($user));
$arrUserPermissions = [];
if (isset($permissions) && count($permissions) > 0) {
foreach ($permissions as $permission) {
$arrUserPermissions[] = [
'permission_id' => $permission,
'user_id' => $user->id,
];
}
// Insert multiple rows at once
DB::table('permission_user')->insert($arrUserPermissions);
//Send confirmation email if requested
/*if (isset($data['confirmation_email']) && $user->confirmed == 0) {
// If user needs confirmation then set email type 1
$email_type = 1;
$input['data']['confirmation_code'] = $user->confirmation_code;
}
// Send email to the user
......@@ -166,7 +161,8 @@ class UserRepository extends BaseRepository
'data' => $input['data'],
'email_template_type' => $email_type,
];
createNotification('', 1, 2, $options);
//createNotification('', 1, 2, $options);*/
return true;
}
......@@ -415,24 +411,40 @@ class UserRepository extends BaseRepository
*/
protected function createUserStub($input)
{
$user = self::MODEL;
$user = new $user();
// $user->name = $input['name'];
$user->first_name = $input['first_name'];
$user->last_name = $input['last_name'];
$user->address = $input['address'];
$user->country_id = 1;
$user->state_id = $input['state_id'];
$user->city_id = $input['city_id'];
$user->zip_code = $input['zip_code'];
$user->ssn = $input['ssn'];
$user->email = $input['email'];
$user->password = bcrypt($input['password']);
$user->status = isset($input['status']) ? 1 : 0;
$user->confirmation_code = md5(uniqid(mt_rand(), true));
$user->confirmed = isset($input['confirmed']) ? 1 : 0;
$user->created_by = access()->user()->id;
$user = self::MODEL;
$user = new $user();
$user->first_name = $input['first_name'];
$user->last_name = $input['last_name'];
$user->email = $input['email'];
$user->password = bcrypt($input['password']);
$user->status = isset($input['status']) ? 1 : 0;
$user->confirmation_code = md5(uniqid(mt_rand(), true));
$user->confirmed = isset($input['confirmed']) ? 1 : 0;
$user->created_by = access()->user()->id;
return $user;
}
/**
* Attach Permission
*
* @param $permissions
* @todo attach permission like role
* @return mix
*/
public function attachPermissions($value='')
{
$arrUserPermissions = [];
if (isset($permissions) && count($permissions) > 0) {
foreach ($permissions as $permission) {
$arrUserPermissions[] = [
'permission_id' => $permission,
'user_id' => $user->id,
];
}
// Insert multiple rows at once
DB::table('permission_user')->insert($arrUserPermissions);
}
}
}
......@@ -22,22 +22,25 @@
</div><!-- /.box-header -->
<div class="box-body">
{{-- First Name --}}
<div class="form-group">
{{ Form::label('name', trans('validation.attributes.backend.access.users.firstName'), ['class' => 'col-lg-2 control-label required']) }}
{{ Form::label('First Name', trans('validation.attributes.backend.access.users.firstName'), ['class' => 'col-lg-2 control-label required']) }}
<div class="col-lg-10">
{{ Form::text('first_name', null, ['class' => 'form-control box-size', 'placeholder' => trans('validation.attributes.backend.access.users.firstName'), 'required' => 'required']) }}
</div><!--col-lg-10-->
</div><!--form control-->
{{-- Last Name --}}
<div class="form-group">
{{ Form::label('name', trans('validation.attributes.backend.access.users.lastName'), ['class' => 'col-lg-2 control-label required']) }}
{{ Form::label('Last Name', trans('validation.attributes.backend.access.users.lastName'), ['class' => 'col-lg-2 control-label required']) }}
<div class="col-lg-10">
{{ Form::text('last_name', null, ['class' => 'form-control box-size', 'placeholder' => trans('validation.attributes.backend.access.users.lastName'), 'required' => 'required']) }}
</div><!--col-lg-10-->
</div><!--form control-->
{{-- Email --}}
<div class="form-group">
{{ Form::label('email', trans('validation.attributes.backend.access.users.email'), ['class' => 'col-lg-2 control-label required']) }}
......@@ -46,6 +49,7 @@
</div><!--col-lg-10-->
</div><!--form control-->
{{-- Password --}}
<div class="form-group">
{{ Form::label('password', trans('validation.attributes.backend.access.users.password'), ['class' => 'col-lg-2 control-label required']) }}
......@@ -54,6 +58,7 @@
</div><!--col-lg-10-->
</div><!--form control-->
{{-- Password Confirmation --}}
<div class="form-group">
{{ Form::label('password_confirmation', trans('validation.attributes.backend.access.users.password_confirmation'), ['class' => 'col-lg-2 control-label required']) }}
......@@ -62,46 +67,7 @@
</div><!--col-lg-10-->
</div><!--form control-->
{{-- address --}}
<div class="form-group">
{{ Form::label('address', trans('validation.attributes.frontend.register-user.address'), ['class' => 'col-lg-2 control-label']) }}
<div class="col-lg-10">
{{ Form::text('address', null, ['class' => 'form-control tinymce box-size', 'placeholder' => trans('validation.attributes.frontend.register-user.address')]) }}
</div><!--col-lg-10-->
</div><!--form-group-->
{{-- state --}}
<div class="form-group">
{{ Form::label('state_id', trans('validation.attributes.frontend.register-user.state'), ['class' => 'col-lg-2 control-label required']) }}
<div class="col-lg-10">
{{ Form::select('state_id', [] , null, ['class' => 'form-control select2 box-size', 'placeholder' => trans('validation.attributes.frontend.register-user.state'), 'id' => 'state', 'required' => 'required']) }}
</div><!--col-lg-10-->
</div><!--form-group-->
{{-- city --}}
<div class="form-group">
{{ Form::label('city_id', trans('validation.attributes.frontend.register-user.city'), ['class' => 'col-lg-2 control-label required']) }}
<div class="col-lg-10">
{{ Form::select('city_id', [], null, ['class' => 'form-control select2 box-size', 'placeholder' => trans('validation.attributes.frontend.register-user.city'), 'id' => 'city', 'required' => 'required']) }}
</div><!--col-lg-10-->
</div><!--form-group-->
{{-- zipcode --}}
<div class="form-group">
{{ Form::label('zip_code', trans('validation.attributes.frontend.register-user.zipcode'), ['class' => 'col-lg-2 control-label required']) }}
<div class="col-lg-10">
{{ Form::input('name', 'zip_code', null, ['class' => 'form-control box-size', 'placeholder' => trans('validation.attributes.frontend.register-user.zipcode'), 'required' => 'required']) }}
</div><!--col-lg-6-->
</div><!--form-group-->
{{-- SSN --}}
<div class="form-group">
{{ Form::label('ssn', trans('validation.attributes.frontend.register-user.ssn'), ['class' => 'col-lg-2 control-label required']) }}
<div class="col-lg-10">
{{ Form::input('name', 'ssn', null, ['class' => 'form-control box-size', 'placeholder' => trans('validation.attributes.frontend.register-user.ssn'), 'required' => 'required']) }}
</div><!--col-lg-6-->
</div><!--form-group-->
{{-- Status --}}
<div class="form-group">
{{ Form::label('status', trans('validation.attributes.backend.access.users.active'), ['class' => 'col-lg-2 control-label']) }}
......@@ -115,6 +81,7 @@
</div><!--col-lg-1-->
</div><!--form control-->
{{-- Confirmed --}}
<div class="form-group">
{{ Form::label('confirmed', trans('validation.attributes.backend.access.users.confirmed'), ['class' => 'col-lg-2 control-label']) }}
......@@ -128,6 +95,7 @@
</div><!--col-lg-1-->
</div><!--form control-->
{{-- Confirmation Email --}}
<div class="form-group">
<label class="col-lg-2 control-label">{{ trans('validation.attributes.backend.access.users.send_confirmation_email') }}<br/>
<small>{{ trans('strings.backend.access.users.if_confirmed_off') }}</small>
......@@ -143,6 +111,7 @@
</div><!--col-lg-1-->
</div><!--form control-->
{{-- Associated Roles --}}
<div class="form-group">
{{ Form::label('status', trans('validation.attributes.backend.access.users.associated_roles'), ['class' => 'col-lg-2 control-label']) }}
......@@ -184,6 +153,7 @@
</div><!--col-lg-3-->
</div><!--form control-->
{{-- Associated Permissions --}}
<div class="form-group">
{{ Form::label('associated-permissions', trans('validation.attributes.backend.access.roles.associated_permissions'), ['class' => 'col-lg-2 control-label']) }}
<div class="col-lg-10">
......@@ -197,6 +167,7 @@
</div><!--col-lg-3-->
</div><!--form control-->
{{-- Buttons --}}
<div class="edit-form-btn">
{{ link_to_route('admin.access.user.index', trans('buttons.general.cancel'), [], ['class' => 'btn btn-danger btn-md']) }}
{{ Form::submit(trans('buttons.general.crud.create'), ['class' => 'btn btn-primary btn-md']) }}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment