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

Adjust to latest atk4/data #99

Merged
merged 3 commits into from
Aug 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ $app->auth = new \Atk4\Login\Auth([
'pageExit' => 'goodbye', // where to send user after logout

// Oter options:
// 'hasUserMenu' => false, // will disable interaction with Admin Layout user menu
// 'hasUserMenu' => false, // will disable interaction with Admin Layout user menu
]);
$app->auth->setModel(new User($app->db));
```
Expand Down
6 changes: 3 additions & 3 deletions demos/_includes/MigratorConsole.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ class MigratorConsole extends Console
/** @const string */
public const HOOK_AFTER_MIGRATION = self::class . '@afterMigration';

/** @var string Name of migrator class to use */
public $migrator_class = Migrator::class;
/** @var class-string */
public string $migratorClass = Migrator::class;

/**
* Provided with array of models, perform migration for each of them.
Expand All @@ -49,7 +49,7 @@ public function migrateModels($models): void
$model->setPersistence($console->getApp()->db);
}

(new $this->migrator_class($model))->dropIfExists()->create(); // recreate table
(new $this->migratorClass($model))->dropIfExists()->create(); // recreate table

$console->debug(' ' . get_class($model) . '.. OK');
}
Expand Down
4 changes: 2 additions & 2 deletions src/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,8 @@ public function setModel(Model $model, string $fieldLogin = null, string $fieldP
protected function loadFromCache(): void
{
$cacheData = $this->cache->getData();
if (isset($cacheData[$this->user->id_field])) {
$this->user = $this->user->getModel()->load($cacheData[$this->user->id_field]);
if (isset($cacheData[$this->user->idField])) {
$this->user = $this->user->getModel()->load($cacheData[$this->user->idField]);
}
$this->user->setMulti($cacheData);
}
Expand Down
18 changes: 9 additions & 9 deletions src/Feature/PasswordManagementTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,20 @@ trait PasswordManagementTrait
{
public function initPasswordManagement(): void
{
$this->addUserAction('generate_random_password', [
$this->addUserAction('generateRandomPassword', [
'appliesTo' => UserAction::APPLIES_TO_NO_RECORDS,
'system' => true,
'args' => [
'length' => ['type' => 'integer'],
],
]);
$this->addUserAction('reset_password', [
$this->addUserAction('resetPassword', [
'appliesTo' => UserAction::APPLIES_TO_SINGLE_RECORD,
'args' => [
'length' => ['type' => 'integer'],
],
]);
$this->addUserAction('check_password_strength', [
$this->addUserAction('checkPasswordStrength', [
// 'appliesTo' => UserAction::APPLIES_TO_NO_RECORDS,
'args' => [
'password' => ['type' => 'string'],
Expand All @@ -38,7 +38,7 @@ public function initPasswordManagement(): void
/**
* Generate random password for the user, returns it.
*/
public function generate_random_password(int $length = 8): string
public function generateRandomPassword(int $length = 8): string
{
return (new PasswordField())->generatePassword($length);
}
Expand All @@ -51,11 +51,11 @@ public function generate_random_password(int $length = 8): string
* if different fieldPassword are set in Auth controller.
* This has to be fixed somehow.
*/
public function reset_password(int $length = 8): string
public function resetPassword(int $length = 8): string
{
$passwordField = PasswordField::assertInstanceOf($this->getField('password'));

$password = $this->generate_random_password($length);
$password = $this->generateRandomPassword($length);

$passwordField->setPassword($this, $password);
$this->save();
Expand Down Expand Up @@ -85,7 +85,7 @@ public function reset_password(int $length = 8): string
*
* @return string|null
*/
public function check_password_strength(string $password, array $settings = ['strength' => 3])
public function checkPasswordStrength(string $password, array $settings = ['strength' => 3])
{
$length = strlen($password);
$nUpper = 0;
Expand All @@ -106,7 +106,7 @@ public function check_password_strength(string $password, array $settings = ['st
}
}

$strength = intdiv($this->calculate_strength($password), 10);
$strength = intdiv($this->calculatePasswordStrength($password), 10);

if (isset($settings['strength']) && $strength < $settings['strength']) {
return 'Password is not strong enough. Make it longer or use more capitals and symbols.';
Expand Down Expand Up @@ -134,7 +134,7 @@ public function check_password_strength(string $password, array $settings = ['st
/**
* Calculate score for a password. Credit: https://gist.github.com/xrstf/2926619.
*/
private function calculate_strength(string $pw): int
private function calculatePasswordStrength(string $pw): int
{
$length = strlen($pw);
$score = $length * 4;
Expand Down
4 changes: 2 additions & 2 deletions src/Feature/SetupUserModelTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ public function setupUserModel(): void
'model' => function ($m) {
return $m->ref('role_id')->ref('AccessRules');
},
'our_field' => 'role_id',
'their_field' => 'role_id',
'ourField' => 'role_id',
'theirField' => 'role_id',
]);
}
}
6 changes: 3 additions & 3 deletions src/Feature/SignupTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,19 @@
trait SignupTrait
{
/**
* Adds register_new_user action.
* Adds registerNewUser action.
*/
public function initSignup(): void
{
$this->addUserAction('register_new_user', ['appliesTo' => UserAction::APPLIES_TO_NO_RECORDS, 'fields' => ['name', 'email', 'password']]);
$this->addUserAction('registerNewUser', ['appliesTo' => UserAction::APPLIES_TO_NO_RECORDS, 'fields' => ['name', 'email', 'password']]);
}

/**
* Creates new user record.
*
* @param array $data Optionally can pass field values of User model
*/
public function register_new_user($data = []): void
public function registerNewUser($data = []): void
{
$this->save($data);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Feature/UniqueFieldValueTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function setUnique(string $fieldName)
if ($entity->isDirty($fieldName)) {
$clonedModel = clone $entity->getModel();
if ($entity->getId() !== null) {
$clonedModel->addCondition($entity->id_field, '!=', $entity->getId());
$clonedModel->addCondition($entity->idField, '!=', $entity->getId());
}
$clonedModel->addCondition($fieldName, $entity->get($fieldName));
if ($clonedModel->action('exists')->getOne()) {
Expand Down
2 changes: 1 addition & 1 deletion src/Form/Control/Fields.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function setModel(Model $model, array $fields = null): void
return [
'value' => $field->shortName,
'title' => $field->getCaption(),
// 'icon' => ($field->shortName == $field->model->id_field ? 'key' : null), // cannot get field->model here :(
// 'icon' => ($field->shortName == $field->model->idField ? 'key' : null), // cannot get field->model here :(
];
};

Expand Down
2 changes: 1 addition & 1 deletion src/Model/AccessRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ protected function init(): void
{
parent::init();

$this->hasOne('role_id', ['model' => [Role::class], 'our_field' => 'role_id', 'their_field' => 'id', 'caption' => 'Role'])
$this->hasOne('role_id', ['model' => [Role::class], 'ourField' => 'role_id', 'theirField' => 'id', 'caption' => 'Role'])
->addTitle();

$this->addField('model'); // model class name
Expand Down
4 changes: 2 additions & 2 deletions src/Model/Role.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ protected function init(): void

$this->addField('name');

$this->hasMany('Users', ['model' => [User::class], 'our_field' => 'id', 'their_field' => 'role_id']);
$this->hasMany('AccessRules', ['model' => [AccessRule::class], 'our_field' => 'id', 'their_field' => 'role_id']);
$this->hasMany('Users', ['model' => [User::class], 'ourField' => 'id', 'theirField' => 'role_id']);
$this->hasMany('AccessRules', ['model' => [AccessRule::class], 'ourField' => 'id', 'theirField' => 'role_id']);

$this->setupRoleModel();
}
Expand Down
2 changes: 1 addition & 1 deletion src/Model/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ protected function init(): void
$this->addField('password', [PasswordField::class]);

// currently user can have only one role. In future it should be n:n relation
$this->hasOne('role_id', ['model' => [Role::class], 'our_field' => 'role_id', 'their_field' => 'id', 'caption' => 'Role'])
$this->hasOne('role_id', ['model' => [Role::class], 'ourField' => 'role_id', 'theirField' => 'id', 'caption' => 'Role'])
->addTitle();

$this->setupUserModel();
Expand Down
4 changes: 2 additions & 2 deletions src/UserAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ protected function init(): void
*/
public function setModel(Model $user): void
{
// $user->getUserAction('register_new_user')->system = true;
// $user->getUserAction('registerNewUser')->system = true;
$user->getUserAction('add')->system = true;

// set model for CRUD
Expand Down Expand Up @@ -65,7 +65,7 @@ public function setModel(Model $user): void
return [
$v->getOwner()->hide(),
new JsToast([
'message' => 'Password for ' . $userEntity->get($userEntity->title_field) . ' is changed!',
'message' => 'Password for ' . $userEntity->get($userEntity->titleField) . ' is changed!',
'class' => 'success',
]),
];
Expand Down
32 changes: 16 additions & 16 deletions tests/Feature/PasswordManagementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,20 @@ public function testGenerateRandomPassword(): void
use PasswordManagementTrait;
};
$model = new $class(new Persistence\Array_());
$this->assertIsString($model->generate_random_password(4));
$this->assertIsString($model->generateRandomPassword(4));
}

public function testBasic(): void
{
$this->setupDefaultDb();
$model = $this->createUserModel();

$this->assertTrue($model->hasUserAction('generate_random_password'));
$this->assertTrue($model->hasUserAction('reset_password'));
$this->assertTrue($model->hasUserAction('check_password_strength'));
$this->assertTrue($model->hasUserAction('generateRandomPassword'));
$this->assertTrue($model->hasUserAction('resetPassword'));
$this->assertTrue($model->hasUserAction('checkPasswordStrength'));

// simply generate password and return it
$this->assertIsString($model->createEntity()->executeUserAction('generate_random_password', 8));
$this->assertIsString($model->createEntity()->executeUserAction('generateRandomPassword', 8));

// generate new password and set model record password field and save it and email if possible
$entity = $model->load(1);
Expand All @@ -44,29 +44,29 @@ public function testBasic(): void
$this->assertIsString($args[2]);
};

$this->assertIsString($pass = $entity->executeUserAction('reset_password', 8));
$this->assertIsString($pass = $entity->executeUserAction('resetPassword', 8));
$this->assertTrue(PasswordField::assertInstanceOf($entity->getField('password'))->verifyPassword($entity, $pass));
$entity->reload();
$this->assertTrue(PasswordField::assertInstanceOf($entity->getField('password'))->verifyPassword($entity, $pass));

// check password strength
$this->assertIsString($entity->executeUserAction('check_password_strength', 'qwerty', ['strength' => 3])); // bad
$this->assertNull($entity->executeUserAction('check_password_strength', 'Qwerty312#~%dsQWRDGFfdfh', ['strength' => 3])); // good
$this->assertIsString($entity->executeUserAction('checkPasswordStrength', 'qwerty', ['strength' => 3])); // bad
$this->assertNull($entity->executeUserAction('checkPasswordStrength', 'Qwerty312#~%dsQWRDGFfdfh', ['strength' => 3])); // good

// check password length
$this->assertIsString($entity->executeUserAction('check_password_strength', 'qwerty', ['len' => 8])); // bad
$this->assertNull($entity->executeUserAction('check_password_strength', 'Qwerty312#~%dsQWRDGFfdfh', ['len' => 8])); // good
$this->assertIsString($entity->executeUserAction('checkPasswordStrength', 'qwerty', ['len' => 8])); // bad
$this->assertNull($entity->executeUserAction('checkPasswordStrength', 'Qwerty312#~%dsQWRDGFfdfh', ['len' => 8])); // good

// check password symbols
$this->assertIsString($entity->executeUserAction('check_password_strength', 'qwerty', ['symbols' => 4])); // bad
$this->assertNull($entity->executeUserAction('check_password_strength', 'Qwerty312##$$%%^^@@fdsfs', ['symbols' => 4])); // good
$this->assertIsString($entity->executeUserAction('checkPasswordStrength', 'qwerty', ['symbols' => 4])); // bad
$this->assertNull($entity->executeUserAction('checkPasswordStrength', 'Qwerty312##$$%%^^@@fdsfs', ['symbols' => 4])); // good

// check password numbers
$this->assertIsString($entity->executeUserAction('check_password_strength', 'qwerty', ['numbers' => 4])); // bad
$this->assertNull($entity->executeUserAction('check_password_strength', 'Qwerty312634dgf#@$', ['numbers' => 4])); // good
$this->assertIsString($entity->executeUserAction('checkPasswordStrength', 'qwerty', ['numbers' => 4])); // bad
$this->assertNull($entity->executeUserAction('checkPasswordStrength', 'Qwerty312634dgf#@$', ['numbers' => 4])); // good

// check password upper letters
$this->assertIsString($entity->executeUserAction('check_password_strength', 'qwerty', ['upper' => 4])); // bad
$this->assertNull($entity->executeUserAction('check_password_strength', 'QwERTYqAZ324', ['upper' => 4])); // good
$this->assertIsString($entity->executeUserAction('checkPasswordStrength', 'qwerty', ['upper' => 4])); // bad
$this->assertNull($entity->executeUserAction('checkPasswordStrength', 'QwERTYqAZ324', ['upper' => 4])); // good
}
}
4 changes: 2 additions & 2 deletions tests/Feature/SignupTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ public function testBasic(): void
$this->setupDefaultDb();
$m = $this->createUserModel();

$this->assertTrue($m->hasUserAction('register_new_user'));
$this->assertTrue($m->hasUserAction('registerNewUser'));

// as result it makes model loaded (as entity) with new user record
$m->createEntity()->executeUserAction(
'register_new_user',
'registerNewUser',
[
'name' => 'New user',
'email' => 'test',
Expand Down