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
5ef62d36
Commit
5ef62d36
authored
Mar 07, 2018
by
cygnet
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
worked for creating access level apis
parent
1dbc10ef
Changes
8
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
359 additions
and
11 deletions
+359
-11
PermissionController.php
app/Http/Controllers/Api/V1/PermissionController.php
+115
-0
RolesController.php
app/Http/Controllers/Api/V1/RolesController.php
+122
-0
UsersController.php
app/Http/Controllers/Api/V1/UsersController.php
+58
-8
PermissionResource.php
app/Http/Resources/PermissionResource.php
+25
-0
RoleResource.php
app/Http/Resources/RoleResource.php
+26
-0
UserResource.php
app/Http/Resources/UserResource.php
+7
-2
UserRepository.php
app/Repositories/Backend/Access/User/UserRepository.php
+1
-1
api.php
routes/api.php
+5
-0
No files found.
app/Http/Controllers/Api/V1/PermissionController.php
0 → 100644
View file @
5ef62d36
<?php
namespace
App\Http\Controllers\Api\V1
;
use
App\Http\Resources\PermissionResource
;
use
App\Models\Access\Permission\Permission
;
use
App\Repositories\Backend\Access\Permission\PermissionRepository
;
use
Illuminate\Http\Request
;
use
Validator
;
class
PermissionController
extends
APIController
{
protected
$repository
;
/**
* __construct.
*
* @param $repository
*/
public
function
__construct
(
PermissionRepository
$repository
)
{
$this
->
repository
=
$repository
;
}
/**
* Return the users.
*
* @return \Illuminate\Http\Response
*/
public
function
index
(
Request
$request
)
{
$limit
=
$request
->
get
(
'paginate'
)
?
$request
->
get
(
'paginate'
)
:
25
;
return
PermissionResource
::
collection
(
$this
->
repository
->
getPaginated
(
$limit
)
);
}
/**
* Return the specified resource.
*
* @param User $user
*
* @return \Illuminate\Http\Response
*/
public
function
show
(
Permission
$permission
)
{
return
new
PermissionResource
(
$permission
);
}
/**
* Creates the Resourse for Role
*
* @param Request $request
*
* @return \Illuminate\Http\Response
*/
public
function
store
(
Request
$request
)
{
$validation
=
$this
->
valiatingRequest
(
$request
);
if
(
$validation
->
fails
())
{
return
$this
->
throwValidation
(
$validation
->
messages
()
->
first
());
}
$this
->
repository
->
create
(
$request
->
all
());
$permission
=
Permission
::
orderBy
(
'created_at'
,
'desc'
)
->
first
();
return
new
PermissionResource
(
$permission
);
}
/**
* @param Role $role
* @param UpdateRoleRequest $request
*
* @return mixed
*/
public
function
update
(
Request
$request
,
Permission
$permission
)
{
$validation
=
$this
->
valiatingRequest
(
$request
);
if
(
$validation
->
fails
())
{
return
$this
->
throwValidation
(
$validation
->
messages
()
->
first
());
}
$this
->
repository
->
update
(
$permission
,
$request
->
all
());
$permission
=
Permission
::
findOrfail
(
$permission
->
id
);
return
new
PermissionResource
(
$permission
);
}
public
function
valiatingRequest
(
Request
$request
)
{
$validation
=
Validator
::
make
(
$request
->
all
(),[
'name'
=>
'required|max:191'
,
'display_name'
=>
'required|max:191'
,
]);
return
$validation
;
}
/**
* @param Role $role
* @param DeleteRoleRequest $request
*
* @return mixed
*/
public
function
destroy
(
Permission
$permission
,
Request
$request
)
{
$this
->
repository
->
delete
(
$permission
);
return
[
"message"
=>
"success"
];
}
}
app/Http/Controllers/Api/V1/RolesController.php
0 → 100644
View file @
5ef62d36
<?php
namespace
App\Http\Controllers\Api\V1
;
use
App\Http\Resources\RoleResource
;
use
App\Models\Access\Role\Role
;
use
App\Repositories\Backend\Access\Role\RoleRepository
;
use
Illuminate\Http\Request
;
use
Validator
;
class
RolesController
extends
APIController
{
protected
$repository
;
/**
* __construct.
*
* @param $repository
*/
public
function
__construct
(
RoleRepository
$repository
)
{
$this
->
repository
=
$repository
;
}
/**
* Return the users.
*
* @return \Illuminate\Http\Response
*/
public
function
index
(
Request
$request
)
{
$limit
=
$request
->
get
(
'paginate'
)
?
$request
->
get
(
'paginate'
)
:
25
;
return
RoleResource
::
collection
(
$this
->
repository
->
getPaginated
(
$limit
)
);
}
/**
* Return the specified resource.
*
* @param User $user
*
* @return \Illuminate\Http\Response
*/
public
function
show
(
Role
$role
)
{
return
new
RoleResource
(
$role
);
}
/**
* Creates the Resourse for Role
*
* @param Request $request
*
* @return \Illuminate\Http\Response
*/
public
function
store
(
Request
$request
)
{
$validation
=
$this
->
valiatingRequest
(
$request
);
if
(
$validation
->
fails
())
{
return
$this
->
throwValidation
(
$validation
->
messages
()
->
first
());
}
$this
->
repository
->
create
(
$request
->
all
());
return
new
RoleResource
(
Role
::
orderBy
(
'created_at'
,
'desc'
)
->
first
());
}
/**
* @param Role $role
* @param UpdateRoleRequest $request
*
* @return mixed
*/
public
function
update
(
Request
$request
,
Role
$role
)
{
$validation
=
$this
->
valiatingRequest
(
$request
);
if
(
$validation
->
fails
())
{
return
$this
->
throwValidation
(
$validation
->
messages
()
->
first
());
}
$this
->
repository
->
update
(
$role
,
$request
->
all
());
$role
=
Role
::
findOrfail
(
$role
->
id
);
return
new
RoleResource
(
$role
);
}
public
function
valiatingRequest
(
Request
$request
)
{
$permissions
=
''
;
if
(
$request
->
post
(
"associated_permissions"
)
!=
'all'
)
{
$permissions
=
'required'
;
}
$validation
=
Validator
::
make
(
$request
->
all
(),
[
'name'
=>
'required|max:191'
,
'permissions'
=>
$permissions
,
]);
return
$validation
;
}
/**
* @param Role $role
* @param DeleteRoleRequest $request
*
* @return mixed
*/
public
function
destroy
(
Role
$role
,
Request
$request
)
{
$this
->
repository
->
delete
(
$role
);
return
[
"message"
=>
"success"
];
}
}
app/Http/Controllers/Api/V1/UsersController.php
View file @
5ef62d36
...
...
@@ -3,9 +3,10 @@
namespace
App\Http\Controllers\Api\V1
;
use
App\Http\Resources\UserResource
;
use
App\Models\User\User
;
use
App\Models\
Access\
User\User
;
use
App\Repositories\Backend\Access\User\UserRepository
;
use
Illuminate\Http\Request
;
use
Illuminate\Validation\Rule
;
use
Validator
;
class
UsersController
extends
APIController
...
...
@@ -45,26 +46,75 @@ class UsersController extends APIController
*/
public
function
show
(
User
$user
)
{
return
new
UserResource
(
$user
);
$data
=
new
UserResource
(
$user
);
$history
[
"history"
]
=
history
()
->
renderEntity
(
'User'
,
$user
->
id
);
$maindata
=
$data
->
toArray
(
$user
);
$maindata
=
array_merge
(
$maindata
,
$history
);
return
$maindata
;
}
/**
* Update the specified resource in storage.
*/
public
function
update
(
Request
$request
,
User
$user
)
{
$validation
=
Validator
::
make
(
$request
->
all
(),
[
'name'
=>
'required'
,
'email'
=>
'email|unique:users,email,'
.
$user
->
id
,
'password'
=>
'nullable|confirmed'
,
]);
$validation
=
$this
->
valiatingRequest
(
$request
,
"edit"
,
$user
->
id
);
if
(
$validation
->
fails
())
{
return
$this
->
throwValidation
(
$validation
->
messages
()
->
first
());
}
$this
->
repository
->
update
(
$user
,
$request
);
$user
=
$this
->
repository
->
update
(
$user
->
id
,
$request
->
all
()
);
$user
=
User
::
findOrfail
(
$user
->
id
);
return
new
UserResource
(
$user
);
}
/**
* Store the specified resource in storage.
*/
public
function
store
(
Request
$request
)
{
$validation
=
$this
->
valiatingRequest
(
$request
);
if
(
$validation
->
fails
())
{
return
$this
->
throwValidation
(
$validation
->
messages
()
->
first
());
}
$this
->
repository
->
create
(
$request
);
return
new
UserResource
(
User
::
orderBy
(
'created_at'
,
'desc'
)
->
first
());
}
/**
* Validation function to validate user input
*/
public
function
valiatingRequest
(
Request
$request
,
$string
=
""
,
$id
=
0
)
{
$password
=
(
$string
==
"edit"
)
?
""
:
"required|min:6|confirmed"
;
$validation
=
Validator
::
make
(
$request
->
all
(),
[
'first_name'
=>
'required|max:255'
,
'last_name'
=>
'required|max:255'
,
'email'
=>
'required|max:255|email|unique:users,email,'
.
$id
,
'password'
=>
$password
,
'assignees_roles'
=>
'required'
,
'permissions'
=>
'required'
,
]);
return
$validation
;
}
/**
* Api to delete the resource
* @param Role $role
* @param DeleteRoleRequest $request
*
* @return mixed
*/
public
function
destroy
(
User
$user
,
Request
$request
)
{
$this
->
repository
->
delete
(
$user
);
return
[
"message"
=>
"success"
];
}
}
app/Http/Resources/PermissionResource.php
0 → 100644
View file @
5ef62d36
<?php
namespace
App\Http\Resources
;
use
Illuminate\Http\Resources\Json\Resource
;
class
PermissionResource
extends
Resource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request
*
* @return array
*/
public
function
toArray
(
$request
)
{
return
[
'id'
=>
$this
->
id
,
'name'
=>
$this
->
name
,
'display_name'
=>
$this
->
display_name
,
'sort'
=>
$this
->
sort
,
];
}
}
app/Http/Resources/RoleResource.php
0 → 100644
View file @
5ef62d36
<?php
namespace
App\Http\Resources
;
use
Illuminate\Http\Resources\Json\Resource
;
class
RoleResource
extends
Resource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request
*
* @return array
*/
public
function
toArray
(
$request
)
{
return
[
'id'
=>
$this
->
id
,
'name'
=>
$this
->
name
,
"permission"
=>
(
$this
->
all
)
?
"All"
:
optional
(
$this
->
permissions
)
->
pluck
(
"display_name"
),
"noofuses"
=>
$this
->
users
->
count
(),
"sort"
=>
$this
->
sort
,
];
}
}
app/Http/Resources/UserResource.php
View file @
5ef62d36
...
...
@@ -17,10 +17,15 @@ class UserResource extends Resource
{
return
[
'id'
=>
$this
->
id
,
'name'
=>
$this
->
name
,
'first_name'
=>
$this
->
first_name
,
'last_name'
=>
$this
->
last_name
,
'email'
=>
$this
->
email
,
'role'
=>
$this
->
roles
()
->
first
()
->
name
,
'confirmed'
=>
$this
->
confirmed
,
'role'
=>
optional
(
$this
->
roles
()
->
first
())
->
name
,
'registered_at'
=>
$this
->
created_at
->
toIso8601String
(),
'last_updated_at'
=>
$this
->
updated_at
->
toIso8601String
(),
];
}
}
app/Repositories/Backend/Access/User/UserRepository.php
View file @
5ef62d36
...
...
@@ -137,7 +137,7 @@ class UserRepository extends BaseRepository
$data
=
$request
->
except
(
'assignees_roles'
,
'permissions'
);
$roles
=
$request
->
get
(
'assignees_roles'
);
$permissions
=
$request
->
get
(
'permissions'
);
$this
->
checkUserByEmail
(
$data
,
$user
);
DB
::
transaction
(
function
()
use
(
$user
,
$data
,
$roles
,
$permissions
)
{
...
...
routes/api.php
View file @
5ef62d36
...
...
@@ -30,5 +30,10 @@ Route::group(['namespace' => 'Api\V1', 'prefix' => 'v1', 'as' => 'v1.'], functio
// Users
Route
::
resource
(
'users'
,
'UsersController'
);
// Roles
Route
::
resource
(
'roles'
,
'RolesController'
);
// Permission
Route
::
resource
(
'permission'
,
'PermissionController'
);
});
});
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