Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
L
laravel-adminpanel
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Administrator
laravel-adminpanel
Commits
7ba1e24f
Commit
7ba1e24f
authored
Nov 26, 2017
by
Viral Solani
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactor User Module - Move db queries
parent
7f7092d0
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
65 additions
and
67 deletions
+65
-67
UserController.php
app/Http/Controllers/Backend/Access/User/UserController.php
+18
-16
BaseModel.php
app/Models/BaseModel.php
+4
-8
UserRepository.php
app/Repositories/Backend/Access/User/UserRepository.php
+43
-37
2017_11_02_060149_create_users_table.php
database/migrations/2017_11_02_060149_create_users_table.php
+0
-6
No files found.
app/Http/Controllers/Backend/Access/User/UserController.php
View file @
7ba1e24f
...
...
@@ -2,18 +2,19 @@
namespace
App\Http\Controllers\Backend\Access\User
;
use
App\Models\Access\User\User
;
use
Illuminate\Support\Facades\DB
;
use
App\Http\Controllers\Controller
;
use
App\Http\Requests\Backend\Access\User\CreateUserRequest
;
use
App\Http\Requests\Backend\Access\User\DeleteUserRequest
;
use
App\Models\Access\Permission\Permission
;
use
App\Repositories\Backend\Access\Role\RoleRepository
;
use
App\Repositories\Backend\Access\User\UserRepository
;
use
App\Http\Requests\Backend\Access\User\EditUserRequest
;
use
App\Http\Requests\Backend\Access\User\ManageUserRequest
;
use
App\Http\Requests\Backend\Access\User\ShowUserRequest
;
use
App\Http\Requests\Backend\Access\User\StoreUserRequest
;
use
App\Http\Requests\Backend\Access\User\CreateUserRequest
;
use
App\Http\Requests\Backend\Access\User\DeleteUserRequest
;
use
App\Http\Requests\Backend\Access\User\ManageUserRequest
;
use
App\Http\Requests\Backend\Access\User\UpdateUserRequest
;
use
App\Models\Access\User\User
;
use
App\Repositories\Backend\Access\Role\RoleRepository
;
use
App\Repositories\Backend\Access\User\UserRepository
;
use
Illuminate\Support\Facades\DB
;
/**
* Class UserController.
...
...
@@ -94,15 +95,16 @@ class UserController extends Controller
*/
public
function
edit
(
User
$user
,
EditUserRequest
$request
)
{
$userPermissions
=
DB
::
table
(
'permission_user'
)
->
where
(
'user_id'
,
$user
->
id
)
->
pluck
(
'permission_id'
,
'permission_id'
)
->
toArray
();
$permissions
=
DB
::
table
(
'permissions'
)
->
pluck
(
'display_name'
,
'id'
)
->
toArray
();
ksort
(
$userPermissions
);
ksort
(
$permissions
);
return
view
(
'backend.access.users.edit'
,
compact
(
'userPermissions'
,
'permissions'
))
->
withUser
(
$user
)
->
withUserRoles
(
$user
->
roles
->
pluck
(
'id'
)
->
all
())
->
withRoles
(
$this
->
roles
->
getAll
());
$permissions
=
Permission
::
getSelectData
(
'display_name'
);
$userPermissions
=
$user
->
permissions
()
->
get
()
->
pluck
(
'id'
)
->
toArray
();
return
view
(
'backend.access.users.edit'
)
->
with
([
'user'
=>
$user
,
'userRoles'
=>
$user
->
roles
->
pluck
(
'id'
)
->
all
(),
'roles'
=>
$this
->
roles
->
getAll
(),
'userPermissions'
=>
$userPermissions
,
'permissions'
=>
$permissions
]);
}
/**
...
...
app/Models/BaseModel.php
View file @
7ba1e24f
...
...
@@ -14,11 +14,11 @@ class BaseModel extends Model
*
* @return array
*/
public
static
function
getSelectData
(
$
id
=
null
,
$val
=
null
)
public
static
function
getSelectData
(
$
field_name
=
'name'
)
{
$collection
=
parent
::
all
();
return
self
::
getItems
(
$collection
);
return
self
::
getItems
(
$collection
,
$field_name
);
}
/**
...
...
@@ -28,24 +28,20 @@ class BaseModel extends Model
*
* @return array
*/
public
static
function
getItems
(
$collection
)
public
static
function
getItems
(
$collection
,
$field_name
)
{
$items
=
[];
foreach
(
$collection
as
$model
)
{
$items
[
$model
->
id
]
=
[
'id'
=>
$model
->
id
,
'name'
=>
$model
->
name
,
'name'
=>
$model
->
$field_
name
,
'model'
=>
$model
,
];
}
foreach
(
$items
as
$id
=>
$item
)
{
if
(
isset
(
static
::
$selectHTMLFormat
)
&&
static
::
$selectHTMLFormat
!==
''
)
{
$items
[
$item
[
'id'
]]
=
static
::
generateSelectName
(
$item
[
'model'
],
static
::
$selectHTMLFormat
);
}
else
{
$items
[
$item
[
'id'
]]
=
$item
[
'name'
];
}
}
return
$items
;
...
...
app/Repositories/Backend/Access/User/UserRepository.php
View file @
7ba1e24f
...
...
@@ -28,50 +28,22 @@ class UserRepository extends BaseRepository
const
MODEL
=
User
::
class
;
/**
* @var
RoleRepository
* @var
User Model
*/
protected
$
role
;
protected
$
model
;
/**
* @param RoleRepository $role
*/
public
function
__construct
(
RoleRepository
$role
)
{
$this
->
role
=
$role
;
}
/**
* @param $permissions
* @param string $by
*
* @return mixed
* @var RoleRepository
*/
public
function
getByPermission
(
$permissions
,
$by
=
'name'
)
{
if
(
!
is_array
(
$permissions
))
{
$permissions
=
[
$permissions
];
}
return
$this
->
query
()
->
whereHas
(
'roles.permissions'
,
function
(
$query
)
use
(
$permissions
,
$by
)
{
$query
->
whereIn
(
'permissions.'
.
$by
,
$permissions
);
})
->
get
();
}
protected
$role
;
/**
* @param $roles
* @param string $by
*
* @return mixed
* @param RoleRepository $role
*/
public
function
getByRole
(
$roles
,
$by
=
'name'
)
public
function
__construct
(
User
$model
,
RoleRepository
$role
)
{
if
(
!
is_array
(
$roles
))
{
$roles
=
[
$roles
];
}
return
$this
->
query
()
->
whereHas
(
'roles'
,
function
(
$query
)
use
(
$roles
,
$by
)
{
$query
->
whereIn
(
'roles.'
.
$by
,
$roles
);
})
->
get
();
$this
->
model
=
$model
;
$this
->
role
=
$role
;
}
/**
...
...
@@ -162,7 +134,7 @@ class UserRepository extends BaseRepository
*
* @return bool
*/
public
function
update
(
Model
$user
,
$request
)
public
function
update
(
$user
,
$request
)
{
$data
=
$request
->
except
(
'assignees_roles'
,
'permissions'
);
$roles
=
$request
->
get
(
'assignees_roles'
);
...
...
@@ -406,4 +378,38 @@ class UserRepository extends BaseRepository
return
$user
;
}
/**
* @param $permissions
* @param string $by
*
* @return mixed
*/
public
function
getByPermission
(
$permissions
,
$by
=
'name'
)
{
if
(
!
is_array
(
$permissions
))
{
$permissions
=
[
$permissions
];
}
return
$this
->
query
()
->
whereHas
(
'roles.permissions'
,
function
(
$query
)
use
(
$permissions
,
$by
)
{
$query
->
whereIn
(
'permissions.'
.
$by
,
$permissions
);
})
->
get
();
}
/**
* @param $roles
* @param string $by
*
* @return mixed
*/
public
function
getByRole
(
$roles
,
$by
=
'name'
)
{
if
(
!
is_array
(
$roles
))
{
$roles
=
[
$roles
];
}
return
$this
->
query
()
->
whereHas
(
'roles'
,
function
(
$query
)
use
(
$roles
,
$by
)
{
$query
->
whereIn
(
'roles.'
.
$by
,
$roles
);
})
->
get
();
}
}
database/migrations/2017_11_02_060149_create_users_table.php
View file @
7ba1e24f
...
...
@@ -18,12 +18,6 @@ class CreateUsersTable extends Migration
$table
->
string
(
'last_name'
,
191
);
$table
->
string
(
'email'
,
191
)
->
unique
();
$table
->
string
(
'password'
,
191
)
->
nullable
();
$table
->
text
(
'address'
,
65535
)
->
nullable
();
$table
->
integer
(
'country_id'
)
->
unsigned
()
->
index
();
$table
->
integer
(
'state_id'
)
->
unsigned
()
->
index
();
$table
->
integer
(
'city_id'
)
->
unsigned
()
->
index
();
$table
->
string
(
'zip_code'
,
191
);
$table
->
string
(
'ssn'
,
191
);
$table
->
boolean
(
'status'
)
->
default
(
1
);
$table
->
string
(
'confirmation_code'
,
191
)
->
nullable
();
$table
->
boolean
(
'confirmed'
)
->
default
(
0
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment