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

Some useful commands for development #292

Merged
merged 2 commits into from
Mar 5, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
51 changes: 51 additions & 0 deletions src/Commands/ActivateUser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php namespace Myth\Auth\Commands;

use CodeIgniter\CLI\BaseCommand;
use CodeIgniter\CLI\CLI;
use Myth\Auth\Models\UserModel;

class ActivateUser extends BaseCommand
{
protected $group = 'Auth';
protected $name = 'auth:activate_user';
protected $description = 'Activate Existing User.';

protected $usage = 'auth:activate_user [identity]';
protected $arguments = [
'identity' => 'User identity.',
];

public function run(array $params = [])
{
// Consume or prompt for password
$identity = array_shift($params);

if (empty($identity))
{
$identity = CLI::prompt('Identity', null, 'required');
}

$type = filter_var($identity, FILTER_VALIDATE_EMAIL) ? 'email' : 'username';

$userModel = new UserModel();
$user = $userModel->where($type, $identity)->first();

if (! $user)
{
CLI::write('User with identity: '. $identity .' not found.', 'red');
}
else
{
$user->active = 1;

if ($userModel->save($user))
{
CLI::write('Sucessfuly activated the user with identity: ' . $identity , 'green');
}
else
{
CLI::write('Failed to activate the user with identity: ' . $identity , 'red');
}
}
}
}
34 changes: 34 additions & 0 deletions src/Commands/HashPassword.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php namespace Myth\Auth\Commands;

use CodeIgniter\CLI\BaseCommand;
use CodeIgniter\CLI\CLI;
use Myth\Auth\Entities\User;

class HashPassword extends BaseCommand
{
protected $group = 'Auth';
protected $name = 'auth:hash_password';
protected $description = 'Hashes given password.';

protected $usage = 'auth:hash_password [password]';
protected $arguments = [
'password' => 'Password value you want to hash.',
];

public function run(array $params = [])
{
// Consume or prompt for password
$password = array_shift($params);

if (empty($password))
{
$password = CLI::prompt('Password', null, 'required');
}

$user = new User();
$user->password = $password;

// write to console
CLI::write('Hash: ' . $user->password_hash, 'green');
}
}
58 changes: 58 additions & 0 deletions src/Commands/SetPassword.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php namespace Myth\Auth\Commands;

use CodeIgniter\CLI\BaseCommand;
use CodeIgniter\CLI\CLI;
use Myth\Auth\Models\UserModel;

class SetPassword extends BaseCommand
{
protected $group = 'Auth';
protected $name = 'auth:set_password';
protected $description = 'Set password to user.';

protected $usage = 'auth:set_password [identity] [password]';
protected $arguments = [
'identity' => 'User identity.',
'password' => 'Password value you want to set.',
];

public function run(array $params = [])
{
// Consume or prompt for password
$identity = isset($params[0]) ? $params[0] : null;
$password = isset($params[1]) ? $params[1] : null;

if (empty($identity))
{
$identity = CLI::prompt('Identity', null, 'required');
}

if (empty($password))
{
$password = CLI::prompt('Password', null, 'required');
}

$type = filter_var($identity, FILTER_VALIDATE_EMAIL) ? 'email' : 'username';

$userModel = new UserModel();
$user = $userModel->where($type, $identity)->first();

if (! $user)
{
CLI::write('User with identity: '. $identity .' not found.', 'red');
}
else
{
$user->password = $password;

if ($userModel->save($user))
{
CLI::write('Password successfully set for user with identity: ' . $identity , 'green');
}
else
{
CLI::write('Failed to set password for user with identity: ' . $identity , 'green');
najdanovicivan marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
}