账号管理 php,管理员admin账号管理工具

管理员admin账号管理工具¶

admin 命令程序用于创建和管理系统超级管理员的账号。

namespace asbamboo\frameworkStandard\command;

use asbamboo\console\ProcessorInterface;

use asbamboo\console\command\CommandAbstract;

use asbamboo\di\ContainerAwareTrait;

use asbamboo\framework\Constant;

use asbamboo\database\Factory;

use asbamboo\frameworkStandard\model\user\UserEntity;

use asbamboo\frameworkStandard\model\user\Constant AS UserConstant;

use asbamboo\database\FactoryInterface;

use asbamboo\database\ManagerInterface;

class AdminCommand extends CommandAbstract

{

use ContainerAwareTrait;

/**

*

* @var ManagerInterface

*/

private $DbManager;

/**

*

* @param FactoryInterface $Db

*/

public function __construct(FactoryInterface $Db)

{

parent::__construct();

$this->DbManager = $Db->getManager();

$this->AddOption('list', null, '列出所有管理员账号', 'l');

$this->AddOption('insert', null, '添加新的管理员账号', 'i');

}

/**

* 列出所有管理员账号

*

* @param ProcessorInterface $Processor

*/

private function lists(ProcessorInterface $Processor) : void

{

/**

*

* @var Factory $Db

*/

$UserEntitys = $this->DbManager->getRepository(UserEntity::class)->findBy(['user_type' => UserConstant::TYPE_ADMIN]);

foreach($UserEntitys AS $UserEntity){

$Processor->output()->print($UserEntity->getUserId(), "\t");

}

}

/**

* 创建新的管理员账号

*

* @param ProcessorInterface $Processor

*/

private function insert(ProcessorInterface $Processor) : void

{

/**

* 用户输入参数

*/

$user_id = $Processor->input()->prompt('请输入管理员账号:');

$user_password = $Processor->input()->prompt('请输入管理员密码:');

$confirm_password = $Processor->input()->prompt('请确认管理员密码:');

/**

* 验证

*/

check:

if($user_password != $confirm_password){

$Processor->output()->print('两次密码输入不一致,请重新确认。', "\r\n");

$user_password = $Processor->input()->prompt('请输入管理员密码:');

$confirm_password = $Processor->input()->prompt('请确认管理员密码:');

goto check;

}

/**

* 管理员实例

*

* @var \asbamboo\frameworkStandard\model\user\UserEntity $UserEntity

*/

$UserEntity = new UserEntity();

$UserEntity->setUserId($user_id);

$UserEntity->setUserPassword($confirm_password);

$UserEntity->setUserType(UserConstant::TYPE_ADMIN);

/**

*

* @var Factory $Db

*/

$this->DbManager->persist($UserEntity);

$this->DbManager->flush();

$Processor->output()->print('管理员添加成功', "\r\n");

}

/**

*

* {@inheritDoc}

* @see \asbamboo\console\command\CommandInterface::exec()

*/

public function exec(ProcessorInterface $Processor)

{

if($this->getOptionValueByProcessor('list', $Processor)){

return $this->lists($Processor);

}

if($this->getOptionValueByProcessor('insert', $Processor)){

return $this->insert($Processor);

}

return $this->insert($Processor);

}

/**

*

* {@inheritDoc}

* @see \asbamboo\console\command\CommandInterface::desc()

*/

public function desc() : string

{

return '管理员账号管理';

}

/**

*

* {@inheritDoc}

* @see \asbamboo\console\command\CommandInterface::help()

*/

public function help() : string

{

$console = $_SERVER['SCRIPT_FILENAME'];

return <<

注意:选项insert和list不能同时使用,如果命令行不带任何参数,则表示省略insert选项。

例: 添加新的管理员账号

php {$console} {$this->getName()} --insert

php {$console} {$this->getName()}

例: 管理员账号列表

php {$console} {$this->getName()} --list

HELP;

}

/**

* 定义命令行名称

*

* @return string

*/

public function getName() : string

{

return 'admin';

}

}

命令的使用¶

./bin/console admin -h 查看帮助信息

./bin/console admin -i 添加新的管理员

./bin/console admin -l 列出管理员名单

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值