在 Cpanel 中部署 Laravel 应用程序后,如何创建第一个具有创建较少用户权限的管理员用户?

发布于 2025-01-09 12:49:37 字数 142 浏览 0 评论 0原文

如果我的 Laravel 应用程序具有基于角色的权限,其中只有一种类型的用户(超级管理员)能够注册用户,我将如何创建该初始用户?

我应该使用播种机并从那里创建用户吗?密码是否会在代码中公开(可能来自环境变量?)?

或者我可以从命令行创建它吗?

If my Laravel app has role-based permissions where only a type of user (superadmin) has the ability to register users, how would I create that initial user?

Should I use a seeder and create the user from there? Would the password be exposed in the code (from env variable maybe?)?

Or could I create it from the command line?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

从﹋此江山别 2025-01-16 12:49:37

就我个人而言,我不喜欢将临时用户存储在 .env 文件中或(上帝禁止)直接存储在 Seeder 中。

我有一个从命令行运行的标准问题用户创建 Artisan 命令。请注意,我还在用户模型上使用了默认情况下对 password 进行哈希处理的变元(请参见答案底部)。

<?php

namespace App\Console\Commands;

use App\Models\User;
use App\Models\Company;
use Illuminate\Console\Command;

class CreateUser extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'app:create-user';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Create a new user';

    /**
     * Execute the console command.
     */
    public function handle(): void
    {
        $email = $this->ask("What's the user's email address?");
        $first_name = $this->ask("What's the user's first name?");
        $last_name = $this->ask("What's the user's last name?");
        $password = $this->secret("What's the user's password?");

        if (User::firstWhere('email', $email)) {
            $this->error('User already exists with that email address!');
            exit;
        }

        $user = User::create([
            'email' => $email,
            'first_name' => $first_name,
            'last_name' => $last_name,
            'password' => $password
        ]);

        $user->assignRole('super-admin');

        $this->info("New user with email address '$email' was successfully created");
    }
}

App\Models\User

<?php

use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticable
{
    protected function password(): Attribute
    {
        return Attribute::make(
            set: fn (string $value) => bcrypt($value)
        );
    }
}

然后您可以从命令行运行:

php artisan app:create-user

并从那里创建您的初始(或任何后续)管理员。

Personally, I don't like to have temporary users stored in the .env files or (god forbid) directly in the Seeder.

I have a standard issue user-creation Artisan command that I run from the command line. Note that I also use a mutator on the User model which hashes password by default (see bottom of answer).

<?php

namespace App\Console\Commands;

use App\Models\User;
use App\Models\Company;
use Illuminate\Console\Command;

class CreateUser extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'app:create-user';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Create a new user';

    /**
     * Execute the console command.
     */
    public function handle(): void
    {
        $email = $this->ask("What's the user's email address?");
        $first_name = $this->ask("What's the user's first name?");
        $last_name = $this->ask("What's the user's last name?");
        $password = $this->secret("What's the user's password?");

        if (User::firstWhere('email', $email)) {
            $this->error('User already exists with that email address!');
            exit;
        }

        $user = User::create([
            'email' => $email,
            'first_name' => $first_name,
            'last_name' => $last_name,
            'password' => $password
        ]);

        $user->assignRole('super-admin');

        $this->info("New user with email address '$email' was successfully created");
    }
}

App\Models\User

<?php

use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticable
{
    protected function password(): Attribute
    {
        return Attribute::make(
            set: fn (string $value) => bcrypt($value)
        );
    }
}

Then you can just run:

php artisan app:create-user

from your command line and create your initial (or any subsequent) admins from there.

万人眼中万个我 2025-01-16 12:49:37

播种是方法。对于密码,只需按照 Laravel 基础框架的相同方式对其进行哈希处理即可。

'password' => Hash::make('my secret password')

当然,密码对于那些有权访问您的源代码的人来说是可见的。当您的网站首次启动并运行时更改密码。

Seeding is the way. For the password just Hash it in the same way the laravel base framework does it.

'password' => Hash::make('my secret password')

Of course, the password will be visible to those with access to your source code. Change the password when your site is up and running the first time.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文