среда, 13 сентября 2017 г.

Laravel. Блог. Присваиваем роли пользователям

Присваиваем роли пользователям.


Идем в модель \app\Model\admin\admin.php
public function roles() {
return $this->belongsToMany('App\Model\admin\role');
}

Идем в контроллер \app\Http\Controllers\Admin\UserController.php

public function store(Request $request)
{
$this->validate($request, [
'name' => 'required|string|max:255',
'email' => 'required|string|email|max:255|unique:admins',
'phone' => 'required|numeric',
'password' => 'required|string|min:6|confirmed',
]);
$request['password'] = bcrypt($request->password);
$user = admin::create($request->all());
$user->roles()->sync($request->role);
return redirect(route('user.index'));
}

public function update(Request $request, $id)
{
$this->validate($request, [
'name' => 'required|string|max:255',
'email' => 'required|string|email|max:255',
'phone' => 'required|numeric',
]);

$user = admin::where('id', $id)->update($request->except('_token','_method', 'role'));
admin::find($id)->roles()->sync($request->role);

return redirect(route('user.index'))->with('message', 'User has been updated successfully');
}

Идем в контроллер \app\Http\Controllers\Admin\RoleController.php
public function store(Request $request)
{
$this->validate($request, [
'name' => 'required|max:50|unique:roles'
]);
$role = new role;
$role->name = $request->name;
$role->save();
$role->permissions()->sync($request->permission);
return redirect(route('role.index'));
}

Изменим страницу редактирования пользвателя \resources\views\admin\user\edit.blade.php
@extends('admin.layouts.app')

@section('main-content')
<div class="content-wrapper">
<!-- Content Header (Page header) -->
<section class="content-header">
<h1>
Text Editors
<small>Advanced form element</small>
</h1>
<ol class="breadcrumb">
<li><a href="#"><i class="fa fa-dashboard"></i> Home</a></li>
<li><a href="#">Forms</a></li>
<li class="active">Editors</li>
</ol>
</section>

<!-- Main content -->
<section class="content">
<div class="row">
<div class="col-md-12">
<!-- general form elements -->
<div class="box box-primary">
<div class="box-header with-border">
<h3 class="box-title">Update Admin</h3>
</div>
<!-- /.box-header -->
@include('includes.messages')
<!-- form start -->
<form role="form" action="{{ route('user.update',$user->id) }}" method="post">
{{ csrf_field() }}
{{ method_field('PUT') }}
<div class="box-body">
<div class="col-lg-offset-3 col-lg-6">
<div class="form-group">
<label for="name">User Name</label>
<input type="text" class="form-control" id="name" name="name" placeholder="User Name" value="{{ $user->name }}">
</div>
<div class="form-group">
<label for="email">Email</label>
<input type="text" class="form-control" id="email" name="email" placeholder="Email" value="{{ $user->email }}">
</div>
<div class="form-group">
<label for="phone">Phone</label>
<input type="text" class="form-control" id="phone" name="phone" placeholder="Phone" value="{{ $user->phone }}">
</div>
<div class="form-group">
<label for="confirm_password">Status</label>
<div class="checkbox">
<label><input type="checkbox" name="status"
@if ($user->status == 1)
checked
@endif
value="1"/>Status</label>
</div>
</div>
<div class="form-group">
<label>Assign Role</label>
<div class="row">
@foreach ($roles as $role)
<div class="col-lg-3">
<div class="checkbox">
<label><input type="checkbox" name="role[]" value="{{ $role->id }}"
@foreach ($user->roles as $user_role)
@if ($user_role->id == $role->id)
checked
@endif
@endforeach
/>{{ $role->name }}</label>
</div>
</div>
@endforeach
</div>
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">Submit</button>
<a type="button" href="{{ route('user.index') }}" class="btn btn-warning">Back</a>
</div>
</div>
</div>
<!-- /.box-body -->
</form>
<!-- /.box -->
</div>
<!-- /.col-->
</div>
<!-- ./row -->
</section>
<!-- /.content -->
</div>
<!-- /.content-wrapper -->

@endsection

Добавим в вид колонку с присвоенными ролями для пользователя.
\resources\views\admin\user\show.blade.php

@extends('admin.layouts.app')

@section('headSection')
<link rel="stylesheet" href="{{ asset('admin/bower_components/datatables.net-bs/css/dataTables.bootstrap.min.css') }}" />
@endsection

@section('main-content')
<div class="content-wrapper">
<!-- Content Header (Page header) -->
<section class="content-header">
<h1>
Blank page
<small>it all starts here</small>
</h1>
<ol class="breadcrumb">
<li><a href="#"><i class="fa fa-dashboard"></i> Home</a></li>
<li><a href="#">Examples</a></li>
<li class="active">Blank page</li>
</ol>
</section>

<!-- Main content -->
<section class="content">

<!-- Default box -->
<div class="box">
<div class="box-header with-border">
<h3 class="box-title">Users</h3>
<a class="col-lg-offset-5 btn btn-success" href="{{ route('user.create') }}">Add New User</a>
@include('includes.messages')

<div class="box-tools pull-right">
<button type="button" class="btn btn-box-tool" data-widget="collapse" data-toggle="tooltip"
title="Collapse">
<i class="fa fa-minus"></i></button>
<button type="button" class="btn btn-box-tool" data-widget="remove" data-toggle="tooltip" title="Remove">
<i class="fa fa-times"></i></button>
</div>
</div>
<div class="box-body">
<div class="box">
<div class="box-header">
<h3 class="box-title">Data Table With Full Features</h3>
</div>
<!-- /.box-header -->
<div class="box-body">
<table id="example1" class="table table-bordered table-striped">
<thead>
<tr>
<th>S.No</th>
<th>User Name</th>
<th>Assigned Roles</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
@foreach ($users as $user)
<tr>
<td>{{ $loop->index + 1 }}</td>
<td>{{ $user->name }}</td>
<td>
@foreach ($user->roles as $role)
{{ $role->name }} |
@endforeach
</td>
<td><a href="{{ route('user.edit', $user->id) }}"><span class="glyphicon glyphicon-edit"></span></a></td>
<td>
<form id="delete-form-{{ $user->id }}" method="post" action="{{ route('user.destroy', $user->id) }}" style="display:none">
{{ csrf_field() }}
{{ method_field('DELETE') }}
</form>
<a href="" onclick="if(confirm('Are you sure, you want to delete this?'))
{event.preventDefault();
document.getElementById('delete-form-{{ $user->id }}').submit();
}else{
event.preventDefault();
}"><span class="glyphicon glyphicon-trash"></span></a>
</td>
</tr>
@endforeach
</tbody>
<tfoot>
<tr>
<th>S.No</th>
<th>User Name</th>
<th>Assigned Roles</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</tfoot>
</table>
</div>
<!-- /.box-body -->
</div>
<!-- /.box -->
</div>
<!-- /.box-body -->
<div class="box-footer">
Footer
</div>
<!-- /.box-footer-->
</div>
<!-- /.box -->

</section>
<!-- /.content -->
</div>
<!-- /.content-wrapper -->
@endsection

@section('footerSection')
<script src="{{ asset('admin/bower_components/datatables.net/js/jquery.dataTables.min.js') }}"></script>
<script src="{{ asset('admin/bower_components/datatables.net-bs/js/dataTables.bootstrap.min.js') }}"></script>
<script>
$(function () {
$('#example1').DataTable()
})
</script>
@endsection

Изменим дату в футере. Идем в resources\views\admin\layouts\footer.blade.php
<strong>Copyright &copy; 2016-{{ Carbon\carbon::now()->year }} <a href="http://bitfumes.com">Bitfumes Webnology</a>.</strong> All rights
reserved.

Идем в модель \app\Model\admin\admin.php
Напишем функцию, которая делает первую букву в имени пользователя большой:
Нам нужны аксессоры.
public function getNameAttribute($value) {
return ucfirst($value);
}

Изменим header в resources\views\admin\layouts\header.blade.php
<header class="main-header">
<!-- Logo -->
<a href="{{ route('admin.home') }}" class="logo">
<!-- mini logo for sidebar mini 50x50 pixels -->
<span class="logo-mini"><b>A</b>LT</span>
<!-- logo for regular state and mobile devices -->
<span class="logo-lg"><b>BitFumes</b> Blog</span>
</a>
<!-- Header Navbar: style can be found in header.less -->
<nav class="navbar navbar-static-top">
<!-- Sidebar toggle button-->
<a href="#" class="sidebar-toggle" data-toggle="push-menu" role="button">
<span class="sr-only">Toggle navigation</span>
</a>

<div class="navbar-custom-menu">
<ul class="nav navbar-nav">
<!-- User Account: style can be found in dropdown.less -->
<li class="dropdown user user-menu">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">
<img src="dist/img/user2-160x160.jpg" class="user-image" alt="User Image">
<span class="hidden-xs">{{ Auth::user()->name }}</span>
</a>
<ul class="dropdown-menu">
<!-- User image -->
<li class="user-header">
<img src="dist/img/user2-160x160.jpg" class="img-circle" alt="User Image">

<p>
{{ Auth::user()->name }}
<small>Member since {{ Auth::user()->created_at->toFormattedDateString() }}</small>
</p>
</li>
<!-- Menu Body -->
<!-- Menu Footer-->
<li class="user-footer">
<div class="btn btn-default pull-right">
<a href="{{ route('logout') }}"
onclick="event.preventDefault();
document.getElementById('logout-form').submit();">
Logout
</a>

<form id="logout-form" action="{{ route('logout') }}" method="POST" style="display: none;">
{{ csrf_field() }}
</form>
</div>
</li>
</ul>
</li>
<!-- Control Sidebar Toggle Button -->
</ul>
</div>
</nav>
</header>

Изменим header в resources\views\admin\layouts\sidebar.blade.php
<aside class="main-sidebar">
<!-- sidebar: style can be found in sidebar.less -->
<section class="sidebar">
<!-- Sidebar user panel -->
<div class="user-panel">
<div class="pull-left image">
<img src="dist/img/user2-160x160.jpg" class="img-circle" alt="User Image">
</div>
<div class="pull-left info">
<p>{{ Auth::user()->name }}</p>
</div>
</div>
<!-- search form -->
<form action="#" method="get" class="sidebar-form">
<div class="input-group">
<input type="text" name="q" class="form-control" placeholder="Search...">
<span class="input-group-btn">
<button type="submit" name="search" id="search-btn" class="btn btn-flat"><i class="fa fa-search"></i>
</button>
</span>
</div>
</form>
<!-- /.search form -->
<!-- sidebar menu: : style can be found in sidebar.less -->
<ul class="sidebar-menu" data-widget="tree">
<li class="header">MAIN NAVIGATION</li>
<li class="active treeview">
<li class=""><a href="{{ route('post.index') }}"><i class="fa fa-circle-o"></i> Posts</a></li>
<li class=""><a href="{{ route('category.index') }}"><i class="fa fa-circle-o"></i> Categories</a></li>
<li class=""><a href="{{ route('tag.index') }}"><i class="fa fa-circle-o"></i> Tags</a></li>
<li class=""><a href="{{ route('user.index') }}"><i class="fa fa-circle-o"></i> Users</a></li>
<li class=""><a href="{{ route('role.index') }}"><i class="fa fa-circle-o"></i> Roles</a></li>
<li class=""><a href="{{ route('permission.index') }}"><i class="fa fa-circle-o"></i> Permissions</a></li>
</li>
</ul>
</section>
<!-- /.sidebar -->
</aside>

Поставим middleware авторизации в роли и разрешения.
\app\Http\Controllers\Admin\RoleController.php
\app\Http\Controllers\Admin\PermissionController.php

public function __construct()
{
$this->middleware('auth:admin');
}


Комментариев нет:

Отправить комментарий

Materialize-css. Футер

Сделаем футер и прижмем к низу страницы. Документация: https://materializecss.com/footer.html