Create settings page for spaces
MODIFY
app/Http/Controllers/SpaceController.php
app/Http/Controllers/SpaceController.php
@@ -8,7 +8,7 @@ use Illuminate\Support\Facades\Auth;
class SpaceController extends Controller
{
- public function __invoke($id)
+ public function show($id)
{
$space = Space::find($id);
@@ -18,4 +18,30 @@ class SpaceController extends Controller
return redirect()->route('dashboard');
}
+
+ public function edit(Space $space)
+ {
+ if (Auth::user()->cant('edit', $space)) {
+ return redirect()->route('settings.spaces.index');
+ }
+
+ return view('spaces.edit', ['space' => $space]);
+ }
+
+ public function update(Request $request, Space $space)
+ {
+ if (Auth::user()->cant('edit', $space)) {
+ return redirect()->route('settings.spaces.index');
+ }
+
+ $request->validate([
+ 'name' => 'required|max:255'
+ ]);
+
+ $space->fill([
+ 'name' => $request->name
+ ])->save();
+
+ return redirect()->route('settings.spaces.index');
+ }
}

MODIFY
app/Models/Space.php
app/Models/Space.php
@@ -20,7 +20,7 @@ class Space extends Model
// Relations
public function users()
{
- return $this->belongsToMany(User::class, 'user_space');
+ return $this->belongsToMany(User::class, 'user_space')->withPivot('role');
}
public function currency()

ADD
app/Policies/SpacePolicy.php
+<?php
+
+namespace App\Policies;
+
+use App\Models\Space;
+use App\Models\User;
+use Illuminate\Auth\Access\HandlesAuthorization;
+
+class SpacePolicy
+{
+ use HandlesAuthorization;
+
+ public function edit(User $user, Space $space): bool
+ {
+ $usersSpace = $user->spaces
+ ->where('id', $space->id)
+ ->first();
+
+ return $usersSpace && $usersSpace->pivot->role === 'admin';
+ }
+}

MODIFY
app/Providers/AuthServiceProvider.php
app/Providers/AuthServiceProvider.php
@@ -14,6 +14,8 @@ use Illuminate\Support\Facades\Gate;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
use App\Policies\RecurringPolicy;
use App\Models\Recurring;
+use App\Models\Space;
+use App\Policies\SpacePolicy;
class AuthServiceProvider extends ServiceProvider
{
@@ -25,6 +27,7 @@ class AuthServiceProvider extends ServiceProvider
protected $policies = [
'App\Model' => 'App\Policies\ModelPolicy',
Earning::class => EarningPolicy::class,
+ Space::class => SpacePolicy::class,
Spending::class => SpendingPolicy::class,
Recurring::class => RecurringPolicy::class,
Tag::class => TagPolicy::class,

MODIFY
resources/assets/sass/components/input.scss
resources/assets/sass/components/input.scss
@@ -156,6 +156,17 @@ button.button {
font-size: 14px;
font-weight: 600;
color: #FFF;
+
+ &.button--secondary {
+ background: #FFF;
+ color: #000;
+ border: 1px solid $colors-light-border;
+ box-shadow: 0 1px 2px 0 rgba(0, 0, 0, 0.05);
+ }
+
+ &.button--small {
+ padding: 5px 10px;
+ }
}
.shadow {

MODIFY
resources/lang/en/general.php
resources/lang/en/general.php
@@ -19,6 +19,9 @@ return [
'account' => 'Account',
'preferences' => 'Preferences',
+ 'general' => 'General',
+ 'members' => 'Members',
+
'empty_state' => 'There aren\'t any :resource yet',
'verify_account' => 'You still need to verify your account&mdash;please check your e-mail',

MODIFY
resources/views/settings/spaces/index.blade.php
resources/views/settings/spaces/index.blade.php
<div class="box">
<ul class="box__section">
@foreach ($spaces as $space)
- <li>{{ $space->name }} &middot; {{ ucfirst($space->pivot->role) }}</li>
+ <li class="row row--middle">
+ <div class="row__column">{{ $space->name }} &middot; {{ ucfirst($space->pivot->role) }}</div>
+ <div class="row__column row__column--compact">
+ @can('edit', $space)
+ <a class="button button--secondary button--small" href="/spaces/{{ $space->id }}/edit">{{ __('pages.settings') }}</a>
+ @endcan
+ </div>
+ </li>
@endforeach
</ul>
</div>

ADD
resources/views/spaces/edit.blade.php
+@extends('layout')
+
+@section('title', __('actions.edit') . ' ' . __('models.space'))
+
+@section('body')
+ <div class="wrapper my-3">
+ <h2 class="mb-3">{{ __('actions.edit') }} {{ __('models.space') }}</h2>
+ <form method="POST" action="/spaces/{{ $space->id }}/update">
+ {{ csrf_field() }}
+ <div class="box">
+ <div class="box__section row">
+ <div class="row__column">
+ <h2>{{ __('general.general') }}</h2>
+ </div>
+ <div class="row__column row__column--double">
+ <div class="input input--small">
+ <label>{{ __('fields.name') }}</label>
+ <input type="text" name="name" value="{{ $space->name }}" />
+ </div>
+ <div class="input input--small mb-0">
+ <label>{{ __('fields.currency') }}</label>
+ <select disabled>
+ <option>{{ $space->currency->name }}</option>
+ </select>
+ <div class="hint mt-05">You cannot modify the currency anymore</div>
+ </div>
+ </div>
+ </div>
+ </div>
+ <div class="row row--right mt-2">
+ <a class="button button--secondary mr-2" href="/settings/spaces">{{ __('actions.cancel') }}</a>
+ <button class="button">{{ __('actions.save') }}</button>
+ </div>
+ </form>
+ <div class="box mt-3">
+ <div class="box__section row">
+ <div class="row__column">
+ <h2>{{ __('general.members') }}</h2>
+ </div>
+ <div class="row__column row__column--double">
+ @foreach ($space->users as $i => $user)
+ <div class="{{ $i > 0 ? 'mt-2' : null }}">
+ <div class="color-dark mb-1">{{ $user->name }}</div>
+ <div class="fs-sm">{{ ucfirst($user->pivot->role) }}</div>
+ </div>
+ @endforeach
+ </div>
+ </div>
+ </div>
+ </div>
+@endsection

MODIFY
routes/web.php
routes/web.php
@@ -120,7 +120,11 @@ Route::group(['middleware' => ['auth']], function () {
Route::get('/settings/spaces', [SettingsController::class, 'getSpaces'])->name('spaces.index');
});
- Route::get('/spaces/{id}', SpaceController::class);
+ Route::name('spaces.')->group(function () {
+ Route::get('/spaces/{space}', [SpaceController::class, 'show'])->name('show');
+ Route::get('/spaces/{space}/edit', [SpaceController::class, 'edit'])->name('edit');
+ Route::post('/spaces/{space}/update', [SpaceController::class, 'update'])->name('update');
+ });
Route::name('ideas.')->group(function () {
Route::get('/ideas/create', [IdeaController::class, 'create'])->name('create');

Create settings page for spaces