Skip to content

Commit

Permalink
Choose model and validation rules. Removed roles() from User at it is…
Browse files Browse the repository at this point in the history
… already

provided by the Shinobi Trait. CDN changed naming for pace themes/templates,
updated master to match.
  • Loading branch information
landjea committed Aug 12, 2016
1 parent 9327d99 commit e70977d
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 25 deletions.
23 changes: 23 additions & 0 deletions src/Config/watchtower.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,29 @@
'site_title' => 'Watchtower',


/*
|--------------------------------------------------------------------------
| Default model to use
|--------------------------------------------------------------------------
|
| By default, watchtower uses its own internal User model. If you have a
| User model you would rather use, provide the name here.
|
| To provide additional Validation rules to the default Update or Store
| form request, place them under the rules heading. By default, the
| rules for password, password confirmation and username are part
| of the request already. But you can override them with yours.
|
*/
'user' => [
'model' => \Smarch\Watchtower\Models\User::class,
'rules' => [
'update' => [],
'store' => [],
],
],


/*
|--------------------------------------------------------------------------
| Default bootstrap theme
Expand Down
40 changes: 28 additions & 12 deletions src/Controllers/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,25 @@
class UserController extends Controller
{

protected $model = \Smarch\Watchtower\Models\User::class;

/**
* Set resource in constructor.
* Set resource model in constructor.
*/
function __construct() {}
function __construct() {
$this->model = $this->getModel();
}


/**
* Determine which model to use
* @return Model Instance
*/
function getModel() {
$model = config('watchtower.user.model', $this->model);
return new $model;
}


/**
* Display a listing of the resource.
Expand All @@ -34,11 +49,11 @@ public function index(Request $request)
if ( Shinobi::can( config('watchtower.acl.user.index', false) ) ) {
if ( $request->has('search_value') ) {
$value = $request->get('search_value');
$users = User::where('name', 'LIKE', '%'.$value.'%')
$users = $this->model::where('name', 'LIKE', '%'.$value.'%')
->orderBy('name')->paginate( config('watchtower.pagination.users', 15) );
session()->flash('search_value', $value);
} else {
$users = User::orderBy('name')->paginate( config('watchtower.pagination.users', 15) );
$users = $this->model::orderBy('name')->paginate( config('watchtower.pagination.users', 15) );
session()->forget('search_value');
}

Expand All @@ -48,6 +63,7 @@ public function index(Request $request)
return view( config('watchtower.views.layouts.unauthorized'), [ 'message' => 'view user list' ]);
}


/**
* Show the form for creating a new resource.
*
Expand All @@ -74,7 +90,7 @@ public function store(UserStoreRequest $request)
$message = " You are not permitted to create users.";

if ( Shinobi::can ( config('watchtower.acl.user.create', false) ) ) {
User::create($request->all());
$this->model::create($request->all());
$level = "success";
$message = "<i class='fa fa-check-square-o fa-1x'></i> Success! User created.";
}
Expand All @@ -92,7 +108,7 @@ public function store(UserStoreRequest $request)
public function show($id)
{
if ( Shinobi::canAtLeast( [ config('watchtower.acl.user.show', false), config('watchtower.acl.user.edit', false) ] ) ) {
$resource = User::findOrFail($id);
$resource = $this->model::findOrFail($id);
$show = "1";
return view( config('watchtower.views.users.show'), compact('resource','show') );
}
Expand All @@ -109,7 +125,7 @@ public function show($id)
public function edit($id)
{
if ( Shinobi::canAtLeast( [ config('watchtower.acl.user.edit', false), config('watchtower.acl.user.show', false) ] ) ) {
$resource = User::findOrFail($id);
$resource = $this->model::findOrFail($id);
$show = "0";
return view( config('watchtower.views.users.edit'), compact('resource','show') );
}
Expand All @@ -129,7 +145,7 @@ public function update($id, UserUpdateRequest $request)
$message = " You are not permitted to update users.";

if ( Shinobi::can ( config('watchtower.acl.user.edit', false) ) ) {
$user = User::findOrFail($id);
$user = $this->model::findOrFail($id);
if ($request->get('password') == '') {
$user->update( $request->except('password') );
} else {
Expand All @@ -155,7 +171,7 @@ public function destroy($id)
$message = " You are not permitted to destroy user objects";

if ( Shinobi::can ( config('watchtower.acl.user.destroy', false) ) ) {
User::destroy($id);
$this->model::destroy($id);
$level = "warning";
$message = "<i class='fa fa-check-square-o fa-1x'></i> Success! User deleted.";
}
Expand All @@ -173,7 +189,7 @@ public function destroy($id)
public function editUserRoles($id)
{
if ( Shinobi::can( config('watchtower.acl.user.role', false) ) ) {
$user = User::findOrFail($id);
$user = $this->model::findOrFail($id);

$roles = $user->roles;

Expand All @@ -199,7 +215,7 @@ public function updateUserRoles($id, Request $request)
$message = " You are not permitted to update user roles.";

if ( Shinobi::can ( config('watchtower.acl.user.role', false) ) ) {
$user = User::findOrFail($id);
$user = $this->model::findOrFail($id);
if ($request->has('ids')) {
$user->roles()->sync( $request->get('ids') );
} else {
Expand All @@ -221,7 +237,7 @@ public function showUserMatrix()
{
if ( Shinobi::can( config('watchtower.acl.user.viewmatrix', false) ) ) {
$roles = Role::all();
$users = User::orderBy('name')->get();
$users = $this->model::orderBy('name')->get();
$us = DB::table('role_user')->select('role_id as r_id','user_id as u_id')->get();

$pivot = [];
Expand Down
8 changes: 0 additions & 8 deletions src/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,4 @@ class User extends Model
* @var array
*/
protected $hidden = ['password', 'remember_token'];

/**
* The roles that have the permissions.
*/
public function roles()
{
return $this->belongsToMany('Smarch\Watchtower\Models\Role');
}
}
7 changes: 5 additions & 2 deletions src/Requests/UserStoreRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,13 @@ public function authorize()
public function rules()
{

return [
$rules = array_merge([
'name' => 'required|max:255|unique:users',
'email' => 'required|email|unique:users',
'password' => 'required|confirmed|min:6',
];
], config('watchtower.user.rules.store') );

return $rules;

}
}
6 changes: 4 additions & 2 deletions src/Requests/UserUpdateRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,13 @@ public function authorize()
public function rules()
{

return [
$rules = array_merge([
'name' => 'required|max:255|unique:users,name,'.$this->user,
'email' => 'required|email|unique:users,email,'.$this->user,
'password' => 'confirmed|min:6',
];
], config('watchtower.user.rules.update') );

return $rules;

}

Expand Down
2 changes: 1 addition & 1 deletion src/Views/layouts/master.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css">

<!-- Pace loader -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/pace/1.0.2/themes/black/pace-theme-big-counter.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/pace/1.0.2/themes/silver/pace-theme-center-circle.min.css">

<!-- Sweetalert (modal) styles -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/1.1.3/sweetalert.min.css">
Expand Down

0 comments on commit e70977d

Please sign in to comment.