Skip to content

Commit

Permalink
rrule policy for editor
Browse files Browse the repository at this point in the history
  • Loading branch information
guoxiangke committed Mar 9, 2020
1 parent ff1610f commit 5c5f2cc
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 14 deletions.
4 changes: 4 additions & 0 deletions app/Forms/RruleForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@

namespace App\Forms;

use Carbon\Carbon;
use Kris\LaravelFormBuilder\Form;

class RruleForm extends Form
{
public function buildForm()
{
$order = $this->getData('entity');
$now = Carbon::now();
$rrule = 'DTSTART:'.$now->format('Ym??')."T??0000Z\nRRULE:FREQ=DAILY;COUNT=?;INTERVAL=1;WKST=MO;BYDAY=MO,TU,WE,TH,FR,SA,SU";
if ($order) {
$this->add('order_id', 'hidden', [
'label' => $order->title,
Expand All @@ -35,6 +38,7 @@ public function buildForm()
->add('string', 'textarea', [
'label' => '计划',
'rules' => 'required',
'value' => $rrule,
'attr' => [
'rows' => 3,
'placeholder' => "DTSTART:20190330T180000Z\nRRULE:FREQ=DAILY;COUNT=5;INTERVAL=1;WKST=MO;BYDAY=TU",
Expand Down
15 changes: 10 additions & 5 deletions app/Http/Controllers/RruleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

namespace App\Http\Controllers;

use App\Forms\Edit\RruleForm as EditForm;
use App\Forms\RruleForm as CreateForm;
use Carbon\Carbon;
use App\Models\Order;
use App\Models\Rrule;
use Carbon\Carbon;
// use App\Repositories\RruleRepository;
use Illuminate\Http\Request;
use App\Forms\RruleForm as CreateForm;
// use App\Repositories\RruleRepository;
use Illuminate\Support\Facades\Session;
use Kris\LaravelFormBuilder\FormBuilder;
use App\Forms\Edit\RruleForm as EditForm;
use Kris\LaravelFormBuilder\FormBuilderTrait;

class RruleController extends Controller
Expand All @@ -25,7 +25,7 @@ class RruleController extends Controller
public function __construct()
{
// $this->repository = $repository;
$this->middleware(['admin']);
// $this->middleware(['admin']);
}

/**
Expand All @@ -38,6 +38,7 @@ public function index()
// $limit = 10;
// $columns = ['*'];
// $rrules = $this->repository->paginate($limit, $columns);
$this->authorize('viewAny', Rrule::class);
$rrules = Rrule::with(
'order',
'order.user',
Expand All @@ -60,6 +61,7 @@ public function index()
*/
public function create(Order $order)
{
$this->authorize('create', Rrule::class);
$form = $this->form(CreateForm::class, [
'method' => 'POST',
'url' => action('RruleController@store'),
Expand All @@ -76,6 +78,7 @@ public function create(Order $order)
*/
public function store(Request $request, FormBuilder $formBuilder)
{
$this->authorize('create', Rrule::class);
$form = $formBuilder->create(CreateForm::class);

if (! $form->isValid()) {
Expand Down Expand Up @@ -119,6 +122,7 @@ public function show(Rrule $rrule)
*/
public function edit(Rrule $rrule)
{
$this->authorize('update', $rrule);
$form = $this->form(
EditForm::class,
[
Expand All @@ -140,6 +144,7 @@ public function edit(Rrule $rrule)
*/
public function update(Request $request, Rrule $rrule, FormBuilder $formBuilder)
{
$this->authorize('update', $rrule);
$form = $this->form(EditForm::class);
// dd($rrule->toArray(),$form->isValid(),$form->getErrors());
if (! $form->isValid()) {
Expand Down
100 changes: 100 additions & 0 deletions app/Policies/RrulePolicy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php

namespace App\Policies;

use App\User;
use App\Models\Rrule;
use Illuminate\Auth\Access\HandlesAuthorization;

// use Spatie\Permission\Models\Permission;
// Permission::create(['name' => 'View any Rrule']);
// Permission::create(['name' => 'Create a Rrule']);
// Permission::create(['name' => 'Update any Rrule']);
// Permission::create(['name' => 'View any ClassRecord']);

class RrulePolicy
{
use HandlesAuthorization;

/**
* Determine whether the user can view any rrules.
*
* @param \App\User $user
* @return mixed
*/
public function viewAny(User $user)
{
return $user->hasAnyPermission(['View any Rrule']);
}

/**
* Determine whether the user can view the rrule.
*
* @param \App\User $user
* @param \App\Models\Rrule $rrule
* @return mixed
*/
public function view(User $user, Rrule $rrule)
{
return $user->hasAnyPermission(['View any Rrule']);
}

/**
* Determine whether the user can create rrules.
*
* @param \App\User $user
* @return mixed
*/
public function create(User $user)
{
return $user->hasAnyPermission(['Create a Rrule']);
}

/**
* Determine whether the user can update the rrule.
*
* @param \App\User $user
* @param \App\Models\Rrule $rrule
* @return mixed
*/
public function update(User $user, Rrule $rrule)
{
return $user->hasAnyPermission(['Update any Rrule']);
}

/**
* Determine whether the user can delete the rrule.
*
* @param \App\User $user
* @param \App\Models\Rrule $rrule
* @return mixed
*/
public function delete(User $user, Rrule $rrule)
{
//
}

/**
* Determine whether the user can restore the rrule.
*
* @param \App\User $user
* @param \App\Models\Rrule $rrule
* @return mixed
*/
public function restore(User $user, Rrule $rrule)
{
//
}

/**
* Determine whether the user can permanently delete the rrule.
*
* @param \App\User $user
* @param \App\Models\Rrule $rrule
* @return mixed
*/
public function forceDelete(User $user, Rrule $rrule)
{
//
}
}
19 changes: 11 additions & 8 deletions app/Providers/AuthServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,25 @@

namespace App\Providers;

use App\Models\Agency;
use App\Models\ClassRecord;
use App\Models\Order;
use App\Models\Profile;
use App\Models\Rrule;
use App\Models\Video;
use App\Models\Agency;
use App\Models\Social;
use App\Models\Profile;
use App\Models\Student;
use App\Models\Teacher;
use App\Models\Video;
use App\Policies\AgencyPolicy;
use App\Policies\ClassRecordPolicy;
use App\Models\ClassRecord;
use App\Policies\OrderPolicy;
use App\Policies\RrulePolicy;
use App\Policies\VideoPolicy;
use App\Policies\AgencyPolicy;
use App\Policies\ProfilePolicy;
use App\Policies\StudentPolicy;
use App\Policies\TeacherPolicy;
use App\Policies\VideoPolicy;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
use App\Policies\ClassRecordPolicy;
use Illuminate\Support\Facades\Gate;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;

class AuthServiceProvider extends ServiceProvider
{
Expand All @@ -36,6 +38,7 @@ class AuthServiceProvider extends ServiceProvider
Profile::class => ProfilePolicy::class,
Video::class => VideoPolicy::class,
Order::class => OrderPolicy::class,
Rrule::class => RrulePolicy::class,
];

/**
Expand Down
20 changes: 19 additions & 1 deletion resources/views/layouts/menu4user.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,22 @@
<li class="nav-item">
<a class="nav-link" href="{{ route('students.recommend') }}">{{__('Recommends')}}</a>
</li>
@endrole
@endrole

@can('View any Order')
<li class="nav-item">
<a class="nav-link" href="{{ route('orders.index') }}">{{__('Order')}}</a>
</li>
@endcan

@can('View any Student')
<li class="nav-item">
<a class="nav-link" href="{{ route('students.index') }}">{{__('Students')}}</a>
</li>
@endcan

@can('View any ClassRecord')
<li class="nav-item">
<a class="nav-link" href="{{ route('classRecords.index') }}">{{__('ClassRecords')}}</a>
</li>
@endcan

0 comments on commit 5c5f2cc

Please sign in to comment.