Skip to content

Commit

Permalink
Add new console command to manage users.
Browse files Browse the repository at this point in the history
issue #94
  • Loading branch information
butschster committed May 6, 2016
1 parent 0ef4cc2 commit cd50433
Show file tree
Hide file tree
Showing 3 changed files with 195 additions and 0 deletions.
11 changes: 11 additions & 0 deletions config/sleeping_owl.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,17 @@

'middleware' => ['web'],

/*
|--------------------------------------------------------------------------
| Authentication default provider
|--------------------------------------------------------------------------
|
| @see config/auth.php : providers
|
*/

'auth_provider' => 'users',

/*
|--------------------------------------------------------------------------
| Path to admin bootstrap files directory
Expand Down
182 changes: 182 additions & 0 deletions src/Commands/UserManagerCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
<?php

namespace SleepingOwl\Admin\Commands;

use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputOption;

class UserManagerCommand extends Command
{

/**
* The console command name.
* @var string
*/
protected $name = 'sleepingowl:user';

/**
* The console command description.
* @var string
*/
protected $description = 'Manage your users.';

public function fire()
{
if ($this->option('create')) {
return $this->createNewUser();
}

if ($this->option('delete')) {
return $this->deleteUser();
}

if ($this->option('password')) {
return $this->changePassword();
}

$this->getUsers();
}

/**
* @return string
* @throws \Exception
*/
public function getUserClass()
{
if (is_null($userClass = config('auth.providers.'.config('sleeping_owl.auth_provider', 'users').'.model'))) {
throw new \Exception('User class not specified in config/auth.php providers.');
}

return $userClass;
}

protected function getUsers()
{
$userClass = $this->getUserClass();

$headers = ['id', 'name', 'email'];
$users = $userClass::get($headers);

$this->table($headers, $users);
}

protected function createNewUser()
{
$userClass = $this->getUserClass();

if (is_null($email = $this->ask('Email'))) {
$this->error('You should specify email.');

return;
}

if (! is_null($userClass::where('email', $email)->first())) {
$this->error("User with same email [{$email}] exists.");

return;
}

if (is_null($password = $this->secret('Password'))) {
$this->error('You should specify password.');

return;
}

$passwordConfirm = $this->secret('Password Confirm');

if ($password !== $passwordConfirm) {
$this->error('Password confirm failed.');

return;
}

$name = $this->ask('User Name');

try {
$user = $userClass::create([
'email' => $email,
'password' => $password,
'name' => $name,
]);

$this->info("User [{$user->id}] created.");
} catch (\Exception $e) {
$this->error('Something went wrong. User not created');

return;
}
}

protected function deleteUser()
{
$userClass = $this->getUserClass();

$this->getUsers();
$id = $this->ask('Select user id to delete');

if (is_null($user = $userClass::find($id))) {
$this->error("User with id [{$id}] not found.");

return;
}

$confirm = $this->confirm("Are you sure want to delete user with id [{$id}]?", false);
if (! $confirm) {
return;
}

$user->delete();
$this->info("User with id [{$id}] was deleted.");
}

/**
* Change administrator's password
*/
protected function changePassword()
{
$userClass = $this->getUserClass();

$this->getUsers();
$id = $this->ask('Select user id to delete');

if (is_null($user = $userClass::find($id))) {
$this->error("User with id [{$id}] not found.");

return;
}

$password = $this->secret('New password');
if (is_null($password)) {
return;
}

$passwordConfirm = $this->secret('Password Confirm');
if (is_null($passwordConfirm)) {
return;
}

if ($password !== $passwordConfirm) {
$this->error('Password confirm failed.');

return;
}

$user->password = bcrypt($password);
$user->save();

$this->info('Password was changed.');
}

/**
* Get the console command options.
* @return array
*/
protected function getOptions()
{
return [
['create', 'c', InputOption::VALUE_NONE, 'Create new user.'],
['delete', 'd', InputOption::VALUE_NONE, 'Delete user.'],
['password', 'p', InputOption::VALUE_NONE, 'Change user password.'],
];
}
}
2 changes: 2 additions & 0 deletions src/Providers/SleepingOwlServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Support\ServiceProvider;
use SleepingOwl\Admin\Commands\InstallCommand;
use SleepingOwl\Admin\Commands\UserManagerCommand;

class SleepingOwlServiceProvider extends ServiceProvider
{
Expand Down Expand Up @@ -44,6 +45,7 @@ protected function registerCommands()
{
$commands = [
InstallCommand::class,
UserManagerCommand::class,
];

foreach ($commands as $command) {
Expand Down

0 comments on commit cd50433

Please sign in to comment.