Skip to content

Commit

Permalink
chore: deleted areas table
Browse files Browse the repository at this point in the history
  • Loading branch information
blt950 committed Aug 2, 2024
1 parent c19a0a0 commit 05be467
Show file tree
Hide file tree
Showing 28 changed files with 48 additions and 263 deletions.
6 changes: 1 addition & 5 deletions app/Http/Controllers/API/EventController.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ public function store(Request $request)
{
$data = $this->validate($request, [
'calendar_id' => 'required|exists:calendars,id',
'area' => 'required|exists:areas,id',
'title' => 'required|string|max:255',
'short_description' => 'nullable|max:280',
'long_description' => 'nullable',
Expand Down Expand Up @@ -89,8 +88,7 @@ public function store(Request $request)
'image' => $imageName,
]);

// Ensure area and user association
$event->area()->associate($request->input('area'));
// Ensure user association
$event->user()->associate($user);
$event->save();

Expand Down Expand Up @@ -121,7 +119,6 @@ public function update(Request $request, Event $event)
{
$data = $this->validate($request, [
'calendar_id' => 'required|exists:calendars,id',
'area' => 'required|exists:areas,id',
'title' => 'required|string|max:255',
'short_description' => 'nullable|max:280',
'long_description' => 'nullable',
Expand Down Expand Up @@ -182,7 +179,6 @@ public function update(Request $request, Event $event)
'image' => $imageURL,
]);

$event->area()->associate($request->input('area'));
$event->user()->associate($user);
$event->save();

Expand Down
17 changes: 4 additions & 13 deletions app/Http/Controllers/EventController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace App\Http\Controllers;

use App\Models\Area;
use App\Models\Calendar;
use App\Models\Event;
use Carbon\Carbon;
Expand Down Expand Up @@ -33,11 +32,9 @@ public function create()
{
$this->authorize('create', Event::class);

$areas = Area::all();

$calendars = Calendar::all();

return view('events.create', compact('areas', 'calendars'));
return view('events.create', compact('calendars'));
}

/**
Expand All @@ -49,7 +46,6 @@ public function store(Request $request)

$this->validate($request, [
'calendar_id' => 'required|exists:calendars,id',
'area' => 'required|exists:areas,id',
'title' => 'required|string|max:255',
'short_description' => 'nullable|max:280',
'long_description' => 'nullable',
Expand Down Expand Up @@ -97,8 +93,7 @@ public function store(Request $request)
'image' => $imageName,
]);

// Ensure area and user association
$event->area()->associate($request->input('area'));
// Ensure user association
$event->user()->associate(\Auth::user());
$event->save();

Expand Down Expand Up @@ -128,11 +123,9 @@ public function edit(Event $event)
{
$this->authorize('update', $event);

$areas = Area::all();

$calendars = Calendar::all();

return view('events.edit', compact('areas', 'calendars', 'event'));
return view('events.edit', compact('calendars', 'event'));
}

/**
Expand All @@ -144,7 +137,6 @@ public function update(Request $request, Event $event)

$this->validate($request, [
'calendar_id' => 'required|exists:calendars,id',
'area' => 'required|exists:areas,id',
'title' => 'required|string|max:255',
'short_description' => 'nullable|max:280',
'long_description' => 'nullable',
Expand Down Expand Up @@ -201,8 +193,7 @@ public function update(Request $request, Event $event)
'image' => $imageURL,
]);

// Ensure area and user association
$event->area()->associate($request->input('area'));
// Ensure user association
$event->user()->associate(\Auth::user());

// Save the event before handling recurrences
Expand Down
5 changes: 1 addition & 4 deletions app/Http/Controllers/StaffingController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace App\Http\Controllers;

use App\Models\Area;
use App\Models\Staffing;
use Carbon\Carbon;
use Illuminate\Http\Request;
Expand Down Expand Up @@ -63,11 +62,9 @@ public function create()
}
}

$areas = Area::all();

$channels = $this->getGuildChannels();

return view('staffing.create', compact('allData', 'areas', 'channels'));
return view('staffing.create', compact('allData', 'channels'));
}

protected function getGuildChannels() {
Expand Down
32 changes: 9 additions & 23 deletions app/Http/Controllers/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace App\Http\Controllers;

use App\Models\Area;
use App\Models\Group;
use App\Models\User;
use Carbon\Carbon;
Expand Down Expand Up @@ -36,11 +35,9 @@ public function show(User $user)
return \Auth::user()->can('view', $event);
});

$areas = Area::all();

$groups = Group::all();

return view('users.show', compact('user', 'events', 'areas', 'groups'));
return view('users.show', compact('user', 'events', 'groups'));
}

/**
Expand All @@ -53,14 +50,8 @@ public function update(Request $request, User $user)
$permissions = [];

// Generate a list of possible validations
foreach (Area::all() as $area) {
foreach (Group::all() as $group) {
// Only process ranks the user is allowed to change
$this->authorize('updateGroup', [$user, $group, $area]);

$key = $area->id . '_' . $group->name;
$permissions[$key] = '';
}
foreach (Group::all() as $group) {
$permissions[$group->name] = '';
}

// Valiate and allow these fields, then loop through permissions to set the final data set
Expand All @@ -69,27 +60,22 @@ public function update(Request $request, User $user)
isset($data[$key]) ? $permissions[$key] = true : $permissions[$key] = false;
}

//dd($permissions);

// Check and update the permissions
foreach ($permissions as $key => $value) {
$str = explode('_', $key);

$area = Area::where('id', $str[0])->get()->first();
$group = Group::where('name', $str[1])->get()->first();
$group = Group::where('name', $key)->get()->first();

// Check if permission is not set, and set it or other way around.
if ($user->groups()->where('area_id', $area->id)->where('group_id', $group->id)->get()->count() == 0) {
if ($user->groups()->where('group_id', $group->id)->get()->count() == 0) {
if ($value == true) {
$this->authorize('updateGroup', [$user, $group, $area]);

// Attach the new permission
$user->groups()->attach($group, ['area_id' => $area->id, 'inserted_by' => \Auth::user()->id]);
$user->groups()->attach($group, ['inserted_by' => \Auth::user()->id]);
}
} else {
if ($value == false) {
$this->authorize('updateGroup', [$user, $group, $area]);

// Detach the permission
$user->groups()->wherePivot('area_id', $area->id)->wherePivot('group_id', $group->id)->detach();
$user->groups()->wherePivot('group_id', $group->id)->detach();
}
}
}
Expand Down
30 changes: 0 additions & 30 deletions app/Models/Area.php

This file was deleted.

11 changes: 2 additions & 9 deletions app/Models/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ class Event extends Model
'recurrence_end_date',
'published',
'image',
'user_id',
'area_id'
'user_id'
];

protected $dates = [
Expand All @@ -43,11 +42,6 @@ public function user()
return $this->belongsTo(User::class);
}

public function area()
{
return $this->belongsTo(Area::class);
}

public function calendar()
{
return $this->belongsTo(Calendar::class);
Expand Down Expand Up @@ -124,8 +118,7 @@ public function generateRecurrences()
'recurrence_unit' => $this->recurrence_unit,
'recurrence_end_date' => $this->recurrence_end_date,
'image' => $this->image,
'user_id' => $this->user_id,
'area_id' => $this->area_id,
'user_id' => $this->user_id
]);

// Move to the next recurrence date
Expand Down
2 changes: 1 addition & 1 deletion app/Models/Group.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ class Group extends Model

public function users()
{
return $this->belongsToMany(User::class, 'permissions')->withPivot('area_id')->withTimestamps();
return $this->belongsToMany(User::class, 'permissions');
}
}
4 changes: 0 additions & 4 deletions app/Models/Staffing.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,4 @@ class Staffing extends Model
'id', 'title', 'date', 'description', 'channel_id', 'message_id', 'week_int', 'section_1_title', 'section_2_title', 'section_3_title', 'section_4_title', 'restrict_bookings'
];

public function area()
{
return $this->belongsTo(Area::class, 'area_id');
}
}
40 changes: 10 additions & 30 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class User extends Authenticatable
*/
public function groups()
{
return $this->belongsToMany(Group::class, 'permissions')->withPivot('area_id')->withTimestamps();
return $this->belongsToMany(Group::class, 'permissions');
}

public function events()
Expand All @@ -69,39 +69,19 @@ public function getNameAttribute()
*
* @return bool
*/
public function isModerator(?Area $area = null)
public function isModerator()
{
if($area == null) {
return $this->groups->where('id', 2)->isNotEmpty();
}

foreach($this->groups->where('id', 2) as $group) {
if($group->pivot->area_id == $area->id) {
return true;
}
}

return false;
return $this->groups->where('id', 2)->isNotEmpty();
}

public function isModeratorOrAbove(?Area $area = null)
/**
* Return if user is a moderator or above
*
* @return bool
*/
public function isModeratorOrAbove()
{
if ($area == null) {
return $this->groups->where('id', '<=', 2)->isNotEmpty();
}

if ($this->isAdmin()) {
return true;
}

// Check if user is moderator or above in the specified area
foreach ($this->groups->where('id', '<=', 2) as $group) {
if ($group->pivot->area_id == $area->id) {
return true;
}
}

return false;
return $this->groups->where('id', '<=', 2)->isNotEmpty();;
}

/**
Expand Down
6 changes: 3 additions & 3 deletions app/Policies/EventPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function index(User $user)

public function view(?User $user, Event $event)
{
return $user && ($user->isModeratorOrAbove() || $user->isModerator($event->area) || $user->is($event->user)) || $event->calendar->public;
return $user && ($user->isModeratorOrAbove() || $user->isModerator() || $user->is($event->user)) || $event->calendar->public;
}

public function create(User $user)
Expand All @@ -27,11 +27,11 @@ public function create(User $user)

public function update(User $user, Event $event)
{
return $user->isModerator($event->area) || $user->isAdmin();
return $user->isModerator() || $user->isAdmin();
}

public function destroy(User $user, Event $event)
{
return $user->isModerator($event->area) || $user->isAdmin();
return $user->isModerator() || $user->isAdmin();
}
}
2 changes: 1 addition & 1 deletion app/Policies/StaffingPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ class StaffingPolicy

public function view(User $user, Staffing $staffing)
{
return $user->isAdmin() || $user->isModerator($staffing->area);
return $user->isAdmin() || $user->isModerator();
}
}
6 changes: 0 additions & 6 deletions app/Policies/UserPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace App\Policies;

use App\Models\Area;
use App\Models\Group;
use App\Models\User;
use Illuminate\Auth\Access\HandlesAuthorization;
Expand Down Expand Up @@ -30,9 +29,4 @@ public function update(User $user, User $model)
{
return $user->isAdmin();
}

public function updateGroup(User $user, User $model)
{
return $user->isAdmin();
}
}
Loading

0 comments on commit 05be467

Please sign in to comment.