dongshan4316 2019-01-14 21:32
浏览 24
已采纳

如何让当前用户执行Laravel命令?

I am trying to build a custom configuration that gets added to my Laravel configuration at boot up. I created an artisan command that allows the developers to place the server into 'test' mode and 'live' mode. This should then force API's to use test keys rather than live keys which works fine.

public function handle()
{
    switch(strtoupper($this->argument('m'))) {
        case 'LIVE':
            \DB::table('server_mode')->where('id', 1)->update([
                'mode' => true,
                'verified_by' => 'Server'
            ]);
            $this->info('Server is now in LIVE mode');
            break;
        case 'TEST':
            \DB::table('server_mode')->where('id', 1)->update([
                'mode' => false,
                'verified_by' => 'Server'
            ]);
            $this->info('Server is now in TEST mode');
            break;
        default:
            $this->error('Please enter either LIVE or TEST');
            break;
    }
}

The problem is, I want to add which developer changed the status. So far, it is hard coded to 'Server'. Is it possible to get the IP or PC information who'm executed the command to store in the DB?

  • 写回答

1条回答 默认 最新

  • duanchai0028 2019-01-14 21:47
    关注

    As said in the comments, Jaquarh doesn't try to authenticate. He just wants to get the username of the perso who ran the command.

    You can use $processUser = exec('whoami'); to get the current user name. (It won't work on windows)

    However, be careful, usernames are not always very informative. My laptop at work came preinstalled with a user name "User".

    Another idea that I can suggest is to force a "name" parameter when using your command.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题