Commit 71e026d7 authored by Viral Solani's avatar Viral Solani

Refactor Update User

parent 41f706cc
...@@ -122,7 +122,7 @@ class UserController extends Controller ...@@ -122,7 +122,7 @@ class UserController extends Controller
*/ */
public function update(User $user, UpdateUserRequest $request) public function update(User $user, UpdateUserRequest $request)
{ {
$this->users->update($user, ['data' => $request->except('assignees_roles'), 'roles' => $request->all('assignees_roles')]); $this->users->update($user, $request);
return redirect()->route('admin.access.user.index')->withFlashSuccess(trans('alerts.backend.users.updated')); return redirect()->route('admin.access.user.index')->withFlashSuccess(trans('alerts.backend.users.updated'));
} }
......
...@@ -30,10 +30,6 @@ class UpdateUserRequest extends Request ...@@ -30,10 +30,6 @@ class UpdateUserRequest extends Request
'email' => 'required|email', 'email' => 'required|email',
'first_name' => 'required', 'first_name' => 'required',
'last_name' => 'required', 'last_name' => 'required',
'state_id' => 'required',
'city_id' => 'required',
'zip_code' => 'required|regex:/^[0-9]+$/',
'ssn' => 'required|regex:/^[0-9]+$/|max:9|min:9',
]; ];
} }
...@@ -45,13 +41,6 @@ class UpdateUserRequest extends Request ...@@ -45,13 +41,6 @@ class UpdateUserRequest extends Request
public function messages() public function messages()
{ {
return [ 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.',
]; ];
} }
} }
...@@ -305,9 +305,23 @@ trait UserAccess ...@@ -305,9 +305,23 @@ trait UserAccess
} }
/** /**
* Alias to eloquent many-to-many relation's detach() method. * Detach multiple permissions from current role.
* *
* @param mixed $permission * @param mixed $permissions
*
* @return void
*/
public function detachPermissions($permissions)
{
foreach ($permissions as $permission) {
$this->detachPermission($permission);
}
}
/**
* Detach permission form current User.
*
* @param object|array $permission
* *
* @return void * @return void
*/ */
...@@ -321,6 +335,7 @@ trait UserAccess ...@@ -321,6 +335,7 @@ trait UserAccess
$permission = $permission['id']; $permission = $permission['id'];
} }
$this->permission()->detach($permission); $this->permissions()->detach($permission);
} }
} }
...@@ -120,27 +120,26 @@ class UserRepository extends BaseRepository ...@@ -120,27 +120,26 @@ class UserRepository extends BaseRepository
public function create($request) public function create($request)
{ {
$data = $request->except('assignees_roles', 'permissions'); $data = $request->except('assignees_roles', 'permissions');
$roles = $request->all('assignees_roles'); $roles = $request->get('assignees_roles');
$permissions = $request->all('permissions') ? $request->all('permissions') : []; $permissions = $request->get('permissions');
$user = $this->createUserStub($data); $user = $this->createUserStub($data);
DB::transaction(function () use ($user, $data, $request) { DB::transaction(function () use ($user, $data, $roles, $permissions) {
// Set email type 2 // Set email type 2
$email_type = 2; $email_type = 2;
if ($user->save()) { if ($user->save()) {
//User Created, Validate Roles //User Created, Validate Roles
if (!count($request->get('assignees_roles'))) { if (!count($roles)) {
throw new GeneralException(trans('exceptions.backend.access.users.role_needed_create')); throw new GeneralException(trans('exceptions.backend.access.users.role_needed_create'));
} }
//Attach new roles //Attach new roles
$user->attachRoles($request->get('assignees_roles')); $user->attachRoles($roles);
// Attach New Permissions // Attach New Permissions
$user->attachPermissions($request->get('permissions')); $user->attachPermissions($permissions);
//$this->attachPermissions($permissions);
//Send confirmation email if requested and account approval is off //Send confirmation email if requested and account approval is off
if (isset($data['confirmation_email']) && $user->confirmed == 0) { if (isset($data['confirmation_email']) && $user->confirmed == 0) {
...@@ -149,24 +148,6 @@ class UserRepository extends BaseRepository ...@@ -149,24 +148,6 @@ class UserRepository extends BaseRepository
event(new UserCreated($user)); event(new UserCreated($user));
//@todo if it is require it can go to event to send notfication
//or user laravel 5.5 notification
//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
$options = [
'data' => $input['data'],
'email_template_type' => $email_type,
];
//createNotification('', 1, 2, $options);*/
return true; return true;
} }
...@@ -176,25 +157,23 @@ class UserRepository extends BaseRepository ...@@ -176,25 +157,23 @@ class UserRepository extends BaseRepository
/** /**
* @param Model $user * @param Model $user
* @param array $input * @param $request
* *
* @throws GeneralException * @throws GeneralException
* *
* @return bool * @return bool
*/ */
public function update(Model $user, array $input) public function update(Model $user, $request)
{ {
$data = $input['data']; $data = $request->except('assignees_roles', 'permissions');
$roles = $input['roles']; $roles = $request->get('assignees_roles');
$permissions = $request->get('permissions');
$permissions = isset($data['permissions']) ? $data['permissions'] : [];
unset($data['permissions']);
$this->checkUserByEmail($data, $user); $this->checkUserByEmail($data, $user);
DB::transaction(function () use ($user, $data, $roles, $permissions) { DB::transaction(function () use ($user, $data, $roles, $permissions) {
if ($user->update($data)) { if ($user->update($data)) {
//For whatever reason this just wont work in the above call, so a second is needed for now
$user->status = isset($data['status']) ? 1 : 0; $user->status = isset($data['status']) ? 1 : 0;
$user->confirmed = isset($data['confirmed']) ? 1 : 0; $user->confirmed = isset($data['confirmed']) ? 1 : 0;
$user->save(); $user->save();
...@@ -202,22 +181,8 @@ class UserRepository extends BaseRepository ...@@ -202,22 +181,8 @@ class UserRepository extends BaseRepository
$this->checkUserRolesCount($roles); $this->checkUserRolesCount($roles);
$this->flushRoles($roles, $user); $this->flushRoles($roles, $user);
$this->flushPermissions($permissions, $user);
event(new UserUpdated($user)); event(new UserUpdated($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')->where('user_id', $user->id)->delete();
DB::table('permission_user')->insert($arrUserPermissions);
}
return true; return true;
} }
...@@ -383,6 +348,8 @@ class UserRepository extends BaseRepository ...@@ -383,6 +348,8 @@ class UserRepository extends BaseRepository
} }
/** /**
* Flush roles out, then add array of new ones
*
* @param $roles * @param $roles
* @param $user * @param $user
*/ */
...@@ -390,7 +357,20 @@ class UserRepository extends BaseRepository ...@@ -390,7 +357,20 @@ class UserRepository extends BaseRepository
{ {
//Flush roles out, then add array of new ones //Flush roles out, then add array of new ones
$user->detachRoles($user->roles); $user->detachRoles($user->roles);
$user->attachRoles($roles['assignees_roles']); $user->attachRoles($roles);
}
/**
* Flush Permissions out, then add array of new ones
*
* @param $permissions
* @param $user
*/
protected function flushPermissions($permissions, $user)
{
//Flush roles out, then add array of new ones
$user->detachPermissions($user->roles);
$user->attachPermissions($permissions);
} }
/** /**
...@@ -402,7 +382,7 @@ class UserRepository extends BaseRepository ...@@ -402,7 +382,7 @@ class UserRepository extends BaseRepository
{ {
//User Updated, Update Roles //User Updated, Update Roles
//Validate that there's at least one role chosen //Validate that there's at least one role chosen
if (count($roles['assignees_roles']) == 0) { if (count($roles) == 0) {
throw new GeneralException(trans('exceptions.backend.access.users.role_needed')); throw new GeneralException(trans('exceptions.backend.access.users.role_needed'));
} }
} }
......
...@@ -28,12 +28,6 @@ class UserTableSeeder extends Seeder ...@@ -28,12 +28,6 @@ class UserTableSeeder extends Seeder
[ [
'first_name' => 'Admin Istrator', 'first_name' => 'Admin Istrator',
'last_name' => 'Admin Istrator', 'last_name' => 'Admin Istrator',
'address' => 'Admin Istrator',
'ssn' => 'Admin Istrator',
'city_id' => '2',
'state_id' => '1',
'country_id' => '1',
'zip_code' => '1123',
'email' => 'admin@admin.com', 'email' => 'admin@admin.com',
'password' => bcrypt('1234'), 'password' => bcrypt('1234'),
'confirmation_code' => md5(uniqid(mt_rand(), true)), 'confirmation_code' => md5(uniqid(mt_rand(), true)),
...@@ -47,12 +41,6 @@ class UserTableSeeder extends Seeder ...@@ -47,12 +41,6 @@ class UserTableSeeder extends Seeder
[ [
'first_name' => 'Backend User', 'first_name' => 'Backend User',
'last_name' => 'Admin Istrator', 'last_name' => 'Admin Istrator',
'address' => 'Admin Istrator',
'ssn' => 'Admin Istrator',
'city_id' => '2',
'state_id' => '1',
'country_id' => '1',
'zip_code' => '1123',
'email' => 'executive@executive.com', 'email' => 'executive@executive.com',
'password' => bcrypt('1234'), 'password' => bcrypt('1234'),
'confirmation_code' => md5(uniqid(mt_rand(), true)), 'confirmation_code' => md5(uniqid(mt_rand(), true)),
...@@ -66,12 +54,6 @@ class UserTableSeeder extends Seeder ...@@ -66,12 +54,6 @@ class UserTableSeeder extends Seeder
[ [
'first_name' => 'Default User', 'first_name' => 'Default User',
'last_name' => 'Admin Istrator', 'last_name' => 'Admin Istrator',
'address' => 'Admin Istrator',
'ssn' => 'Admin Istrator',
'city_id' => '2',
'state_id' => '1',
'country_id' => '1',
'zip_code' => '1123',
'email' => 'user@user.com', 'email' => 'user@user.com',
'password' => bcrypt('1234'), 'password' => bcrypt('1234'),
'confirmation_code' => md5(uniqid(mt_rand(), true)), 'confirmation_code' => md5(uniqid(mt_rand(), true)),
......
...@@ -22,6 +22,7 @@ ...@@ -22,6 +22,7 @@
</div><!-- /.box-header --> </div><!-- /.box-header -->
<div class="box-body"> <div class="box-body">
{{-- First Name --}}
<div class="form-group"> <div class="form-group">
{{ Form::label('name', trans('validation.attributes.backend.access.users.firstName'), ['class' => 'col-lg-2 control-label required']) }} {{ Form::label('name', trans('validation.attributes.backend.access.users.firstName'), ['class' => 'col-lg-2 control-label required']) }}
...@@ -30,6 +31,7 @@ ...@@ -30,6 +31,7 @@
</div><!--col-lg-10--> </div><!--col-lg-10-->
</div><!--form control--> </div><!--form control-->
{{-- Last Name --}}
<div class="form-group"> <div class="form-group">
{{ Form::label('name', trans('validation.attributes.backend.access.users.lastName'), ['class' => 'col-lg-2 control-label required']) }} {{ Form::label('name', trans('validation.attributes.backend.access.users.lastName'), ['class' => 'col-lg-2 control-label required']) }}
...@@ -38,6 +40,7 @@ ...@@ -38,6 +40,7 @@
</div><!--col-lg-10--> </div><!--col-lg-10-->
</div><!--form control--> </div><!--form control-->
{{-- Email --}}
<div class="form-group"> <div class="form-group">
{{ Form::label('email', trans('validation.attributes.backend.access.users.email'), ['class' => 'col-lg-2 control-label required']) }} {{ Form::label('email', trans('validation.attributes.backend.access.users.email'), ['class' => 'col-lg-2 control-label required']) }}
...@@ -46,46 +49,7 @@ ...@@ -46,46 +49,7 @@
</div><!--col-lg-10--> </div><!--col-lg-10-->
</div><!--form control--> </div><!--form control-->
{{-- address --}} {{-- Status --}}
<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::input('textarea', 'address', null, ['class' => 'form-control box-size', 'placeholder' => trans('validation.attributes.frontend.register-user.address'), 'rows' => '3']) }}
</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-->
@if ($user->id != 1) @if ($user->id != 1)
<div class="form-group"> <div class="form-group">
{{ Form::label('status', trans('validation.attributes.backend.access.users.active'), ['class' => 'col-lg-2 control-label']) }} {{ Form::label('status', trans('validation.attributes.backend.access.users.active'), ['class' => 'col-lg-2 control-label']) }}
...@@ -99,6 +63,7 @@ ...@@ -99,6 +63,7 @@
</div><!--col-lg-1--> </div><!--col-lg-1-->
</div><!--form control--> </div><!--form control-->
{{-- Confirmed --}}
<div class="form-group"> <div class="form-group">
{{ Form::label('confirmed', trans('validation.attributes.backend.access.users.confirmed'), ['class' => 'col-lg-2 control-label']) }} {{ Form::label('confirmed', trans('validation.attributes.backend.access.users.confirmed'), ['class' => 'col-lg-2 control-label']) }}
...@@ -112,6 +77,7 @@ ...@@ -112,6 +77,7 @@
</div><!--col-lg-1--> </div><!--col-lg-1-->
</div><!--form control--> </div><!--form control-->
{{-- Associated Roles --}}
<div class="form-group"> <div class="form-group">
{{ Form::label('status', trans('validation.attributes.backend.access.users.associated_roles'), ['class' => 'col-lg-2 control-label']) }} {{ Form::label('status', trans('validation.attributes.backend.access.users.associated_roles'), ['class' => 'col-lg-2 control-label']) }}
...@@ -153,6 +119,7 @@ ...@@ -153,6 +119,7 @@
</div><!--col-lg-3--> </div><!--col-lg-3-->
</div><!--form control--> </div><!--form control-->
{{-- Associated Permissions --}}
<div class="form-group"> <div class="form-group">
{{ Form::label('associated-permissions', trans('validation.attributes.backend.access.roles.associated_permissions'), ['class' => 'col-lg-2 control-label']) }} {{ Form::label('associated-permissions', trans('validation.attributes.backend.access.roles.associated_permissions'), ['class' => 'col-lg-2 control-label']) }}
<div class="col-lg-10"> <div class="col-lg-10">
...@@ -199,63 +166,12 @@ ...@@ -199,63 +166,12 @@
{{ Html::script('js/backend/access/users/script.js') }} {{ Html::script('js/backend/access/users/script.js') }}
<script type="text/javascript"> <script type="text/javascript">
$(document).ready(function() { jQuery(document).ready(function() {
//Getting States of default contry
ajaxCall("{{route('admin.get.states')}}");
Backend.Access.init(); Backend.Access.init();
//Getting Cities of select State
$("#state").on("change", function() {
var stateId = $(this).val();
var url = "{{route('admin.get.cities')}}";
ajaxCall(url, stateId);
});
function ajaxCall(url, data = null)
{
$.ajax({
url: url,
type: "POST",
data: {stateId: data},
success: function(result) {
if(result != null)
{
if(result.status == "city")
{
var userCity = "{{ $user->city_id }}";
var options;
$.each(result.data, function(key, value) {
if(key == userCity)
options += "<option value='" + key + "' selected>" + value + "</option>";
else
options += "<option value='" + key + "'>" + value + "</option>";
});
$("#city").html('');
$("#city").append(options);
}
else
{
var userState = "{{ $user->state_id }}";
var options;
$.each(result.data, function(key, value) {
if(key == userState)
options += "<option value='" + key + "' selected>" + value + "</option>";
else
options += "<option value='" + key + "'>" + value + "</option>";
});
$("#state").append(options);
$("#state").trigger('change');
}
}
}
});
}
/** /**
* This function is used to get clicked element role id and return required result * This function is used to get clicked element role id and return required result
*/ */
$('.get-role-for-permissions').click(function () { jQuery('.get-role-for-permissions').click(function () {
$.ajax({ $.ajax({
type: "POST", type: "POST",
url: "{{ route('admin.get.permission') }}", url: "{{ route('admin.get.permission') }}",
......
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