Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auth customization features #257

Merged
merged 2 commits into from
May 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions config/moonshine.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@
'use_notifications' => true,
'auth' => [
'enable' => true,
'fields' => [
'username' => 'email',
'password' => 'password',
'name' => 'name',
'avatar' => 'avatar'
],
'guard' => 'moonshine',
'guards' => [
'moonshine' => [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use MoonShine\MoonShineAuth;

return new class extends Migration
{
return new class extends Migration {
/**
* Run the migrations.
*
Expand All @@ -17,16 +15,9 @@ public function up()
Schema::create('moonshine_change_logs', function (Blueprint $table) {
$table->id();

$table->foreignId('moonshine_user_id')
->constrained(
MoonShineAuth::model()?->getTable(),
MoonShineAuth::model()?->getKeyName()
)
->cascadeOnDelete()
->cascadeOnUpdate();
$table->foreignId('moonshine_user_id');

$table->integer('changelogable_id');
$table->string('changelogable_type');
$table->morphs('changelogable');

$table->longText('states_before');
$table->longText('states_after');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,14 @@
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use MoonShine\MoonShineAuth;

return new class extends Migration
{
return new class extends Migration {
public function up(): void
{
Schema::create('moonshine_socialites', function (Blueprint $table) {
$table->id();

$table->foreignId('moonshine_user_id')
->constrained(
MoonShineAuth::model()?->getTable(),
MoonShineAuth::model()?->getKeyName()
)
->cascadeOnDelete()
->cascadeOnUpdate();
$table->foreignId('moonshine_user_id');

$table->string('driver');
$table->string('identity');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,14 @@
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use MoonShine\Models\MoonshineUser;
use MoonShine\MoonShineAuth;

return new class extends Migration
{
return new class extends Migration {
public function up()
{
Schema::create('moonshine_user_permissions', function (Blueprint $table) {
$table->id();

$table->foreignId('moonshine_user_id')
->constrained(
MoonShineAuth::model()?->getTable(),
MoonShineAuth::model()?->getKeyName()
)
->cascadeOnDelete()
->cascadeOnUpdate();
$table->foreignId('moonshine_user_id');

$table->json('permissions');

Expand Down
1 change: 1 addition & 0 deletions lang/en/ui.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
'remember_me' => 'Remember me',
'login' => 'Log in',
'logout' => 'Log out',
'username' => 'Username',
'email' => 'E-mail',
'password' => 'Password',
'or_socials' => 'or',
Expand Down
1 change: 1 addition & 0 deletions lang/ru/ui.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
'remember_me' => 'Запомнить',
'login' => 'Войти',
'logout' => 'Выйти',
'username' => 'Логин',
'email' => 'E-mail',
'password' => 'Пароль',
'or_socials' => 'или',
Expand Down
16 changes: 8 additions & 8 deletions resources/views/auth/login.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,19 @@ class="authentication-form"
>
<div class="form-flex-col">
<x-moonshine::form.input-wrapper
name="email"
label="{{ trans('moonshine::ui.login.email') }}"
name="username"
label="{{ trans('moonshine::ui.login.username') }}"
required
>
<x-moonshine::form.input
id="login"
type="email"
name="email"
@class(['form-invalid' => $errors->has('email')])
placeholder="{{ trans('moonshine::ui.login.email') }}"
id="username"
type="username"
name="username"
@class(['form-invalid' => $errors->has('username')])
placeholder="{{ trans('moonshine::ui.login.username') }}"
required
autofocus
value="{{ old('email') }}"
value="{{ old('username') }}"
autocomplete="username"
/>
</x-moonshine::form.input-wrapper>
Expand Down
7 changes: 2 additions & 5 deletions resources/views/form-components/permission.blade.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@if($item->exists && $item instanceof \MoonShine\Models\MoonshineUser)
@if($item->exists)
<div>
<div class="text-lg my-4">{{ $element->label() }}</div>

Expand All @@ -25,10 +25,7 @@ class="form-group-inline"
type="checkbox"
name="permissions[{{ get_class($resource) }}][{{ $ability }}]"
value="1"
:checked="($item->moonshineUserPermission
&& $item->moonshineUserPermission->permissions->has(get_class($resource))
&& isset($item->moonshineUserPermission->permissions[get_class($resource)][$ability])
)"
:checked="$resource->checkUserPermissions($item, $ability)"

/>
</x-moonshine::form.input-wrapper>
Expand Down
25 changes: 14 additions & 11 deletions resources/views/profile.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,26 @@
name="name"
@class(['form-invalid' => $errors->has('name')])
placeholder="{{ trans('moonshine::ui.resource.name') }}"
value="{{ old('name', auth(config('moonshine.auth.guard'))->user()->name) }}"
value="{{ old('name', auth(config('moonshine.auth.guard'))->user()
->{config('moonshine.auth.fields.name', 'name')}) }}"
/>
</x-moonshine::form.input-wrapper>

<x-moonshine::form.input-wrapper
name="email"
label="{{ trans('moonshine::ui.resource.email') }}"
name="username"
label="{{ trans('moonshine::ui.login.username') }}"
required
>
<x-moonshine::form.input
id="email"
type="email"
name="email"
@class(['form-invalid' => $errors->has('email')])
placeholder="{{ trans('moonshine::ui.resource.email') }}"
autocomplete="email"
id="username"
type="text"
name="username"
@class(['form-invalid' => $errors->has('username')])
placeholder="{{ trans('moonshine::ui.login.username') }}"
autocomplete="username"
required
value="{{ old('email', auth(config('moonshine.auth.guard'))->user()->email) }}"
value="{{ old('username', auth(config('moonshine.auth.guard'))->user()
->{config('moonshine.auth.fields.username', 'email')}) }}"
/>
</x-moonshine::form.input-wrapper>

Expand Down Expand Up @@ -73,7 +75,8 @@
id="avatar"
@class(['form-invalid' => $errors->has('avatar')])
placeholder="{{ trans('moonshine::ui.resource.avatar') }}"
:files="[auth(config('moonshine.auth.guard'))->user()->avatar ?? null]"
:files="[auth(config('moonshine.auth.guard'))->user()
->{config('moonshine.auth.fields.avatar', 'avatar')} ?? null]"
dir="moonshine_users"
:path="Storage::disk('public')->url('/')"
:removable="true"
Expand Down
39 changes: 27 additions & 12 deletions src/Commands/InstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,28 @@ public function handle(): void
{
$this->components->info('MoonShine installation ...');

$this->initVendorPublish();
$this->initStorage();
$this->initServiceProvider();
$this->initDirectories();
$this->initDashboard();
$this->initMigrations();

$this->components->info('Installation completed');

if (! app()->runningUnitTests()) {
$this->choice('Can you quickly star our GitHub repository? 🙏🏻', [
'yes', 'no',
], 'yes');

$this->components->bulletList([
'Star or contribute to MoonShine: https://github.com/moonshine-software/moonshine',
'MoonShine Documentation: https://moonshine.cutcode.dev',
'CutCode: https://cutcode.dev',
]);
}

$this->components->task('');
$this->components->info("Now run 'php artisan moonshine:user'");
}

Expand All @@ -40,37 +56,36 @@ protected function initDirectories(): void
$this->makeDir($this->getDirectory() . '/Resources');

$this->components->task('Resources directory created');
}

protected function initVendorPublish(): void
{
Artisan::call('vendor:publish', [
'--provider' => MoonShineServiceProvider::class,
'--force' => true,
]);

$this->components->task('Vendor published');


}

protected function initStorage(): void
{
Artisan::call('storage:link');

$this->components->task('Storage link created');
}

protected function initMigrations(): void
{
if (config('moonshine.use_migrations', true)) {
Artisan::call('migrate');

$this->components->task('Tables migrated');
} else {
$this->components->task('Installed without default migrations');
}

if (! app()->runningUnitTests()) {
$this->choice('Can you quickly star our GitHub repository? 🙏🏻', [
'yes', 'no',
], 'yes');

$this->components->bulletList([
'Star or contribute to MoonShine: https://github.com/moonshine-software/moonshine',
'MoonShine Documentation: https://moonshine.cutcode.dev',
'CutCode: https://cutcode.dev',
]);
}
}

/**
Expand Down
10 changes: 5 additions & 5 deletions src/Commands/UserCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ class UserCommand extends MoonShineCommand

public function handle(): void
{
$email = $this->ask('Email');
$username = $this->ask('Username('.config('moonshine.auth.fields.username', 'email').')');
$name = $this->ask('Name');
$password = $this->secret('Password');

if ($email && $name && $password) {
if ($username && $name && $password) {
MoonShineAuth::model()->query()->create([
'email' => $email,
'name' => $name,
'password' => Hash::make($password),
config('moonshine.auth.fields.username', 'email') => $username,
config('moonshine.auth.fields.name', 'name') => $name,
config('moonshine.auth.fields.password', 'password') => Hash::make($password),
]);

$this->components->info('User is created');
Expand Down
12 changes: 8 additions & 4 deletions src/Http/Controllers/ProfileController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,25 @@ class ProfileController extends BaseController
public function store(ProfileFormRequest $request): RedirectResponse
{
$data = $request->validated();
$resultData = [
config('moonshine.auth.fields.username', 'email') => $data['username'],
config('moonshine.auth.fields.name', 'name') => $data['name'],
];

if (isset($data['password']) && $data['password'] !== '') {
$data['password'] = Hash::make($data['password']);
$resultData[config('moonshine.auth.fields.password', 'password')] = Hash::make($data['password']);
} else {
unset($data['password']);
}

if ($request->hasFile('avatar')) {
$data['avatar'] = $request->file('avatar')
$resultData[config('moonshine.auth.fields.avatar', 'avatar')] = $request->file('avatar')
->store('moonshine_users', 'public');
} else {
$data['avatar'] = $request->get('hidden_avatar');
$resultData[config('moonshine.auth.fields.avatar', 'avatar')] = $request->get('hidden_avatar');
}

$request->user()->update($data);
$request->user()->update($resultData);

return back();
}
Expand Down
5 changes: 0 additions & 5 deletions src/Http/Middleware/Authenticate.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,10 @@

use Closure;

use function config;

use Illuminate\Http\Request;

use MoonShine\MoonShineAuth;

use function redirect;
use function route;

class Authenticate
{
public function handle($request, Closure $next)
Expand Down
Loading