Exactly, it is a simple string with the name of the role.
if ($model->load(Yii::$app->request->post())) {
if($model->save()){
/* get Role from its name : assuming 'auth_level' is the name of a role */
$role = Yii::$app->authManager->getItem($model->auth_level);
/* assign the role to the user */
Yii::$app->authManager->assign($role, $model->ID);
}
return $this->redirect(['view', 'id' => $model->ID]);
}
Missing function getItem, in the documentation it is getRole, however, I tried it and the assign() returns an error for empty value.
Checked the values for the following code:
if ($model->load(Yii::$app->request->post())) {
if($model->save()){
/* get Role from its name : assuming 'auth_level' is the name of a role */
$role = Yii::$app->authManager->getRole($model->auth_level);
var_dump($role);
Yii::$app->end();
/* assign the role to the user */
Yii::$app->authManager->assign($role, $model->ID);
}
$role is a big fat āNULLā.
$_POST has the āauth_levelā set, still after populating the model it becomes BaseActiverRecord:private
$_POST = [
'_csrf' => 'WTZWeWptck5teWdNKz8gKClwZjE8GkU5LVESFVMfO2NubC4DDiEQFg==',
'MyUsers' => [
'username' => 'sass',
'employee_ID' => 'xyz123',
'name' => 'Kekeszes Tej',
'email' => 'dev@test.com',
'auth_level' => 'Level4',
'password' => 'asd',
'password_repeat' => 'asd',
],
];
if ($model->load(Yii::$app->request->post())) {
print_r($model);
echo '<br><br>';
var_dump($model->auth_level);
Yii::$app->end();
print_r($model);
app\models\MyUsers Object ( [password_repeat] => asd [changes] => [auth_level] => [_attributes:yii\db\BaseActiveRecord:private] => Array ( [username] => sass [employee_ID] => xyz123 [name] => Kekeszes Tej [email] => dev@hu.dsv.com [password] => asd )
var_dump($model->auth_level);
NULL
By the way, thanks for warning me to use the AuthManager, I donāt know why I wanted to link relations on the first place.
Here is the full print_r with validations etc.
app\models\MyUsers Object
(
[password_repeat] => asd
[changes] =>
[auth_level] =>
[_attributes:yii\db\BaseActiveRecord:private] => Array
(
[username] => sass
[employee_ID] => xyz123
[name] => Kekeszes Tej
[email] => dev@test.com
[password] => asd
)
[_oldAttributes:yii\db\BaseActiveRecord:private] =>
[_related:yii\db\BaseActiveRecord:private] => Array
(
)
[_errors:yii\base\Model:private] =>
[_validators:yii\base\Model:private] => ArrayObject Object
(
[storage:ArrayObject:private] => Array
(
[0] => yii\validators\RequiredValidator Object
(
[skipOnEmpty] =>
[requiredValue] =>
[strict] =>
[message] => {attribute} cannot be blank.
[attributes] => Array
(
[0] => username
[1] => password
[2] => password_repeat
[3] => employee_ID
[4] => email
[5] => name
)
[on] => Array
(
)
[except] => Array
(
[0] => change
)
[skipOnError] => 1
[enableClientValidation] => 1
[isEmpty] =>
[when] =>
[whenClient] =>
[_events:yii\base\Component:private] => Array
(
)
[_behaviors:yii\base\Component:private] =>
)
[1] => yii\validators\RequiredValidator Object
(
[skipOnEmpty] =>
[requiredValue] =>
[strict] =>
[message] => {attribute} cannot be blank.
[attributes] => Array
(
[0] => password
[1] => password_repeat
)
[on] => Array
(
[0] => change
)
[except] => Array
(
)
[skipOnError] => 1
[enableClientValidation] => 1
[isEmpty] =>
[when] =>
[whenClient] =>
[_events:yii\base\Component:private] => Array
(
)
[_behaviors:yii\base\Component:private] =>
)
[2] => yii\validators\StringValidator Object
(
[length] =>
[max] => 255
[min] =>
[message] => {attribute} must be a string.
[tooShort] =>
[tooLong] => {attribute} should contain at most {max, number} {max, plural, one{character} other{characters}}.
[notEqual] =>
[encoding] => utf-8
[attributes] => Array
(
[0] => username
[1] => password
[2] => password_repeat
)
[on] => Array
(
)
[except] => Array
(
)
[skipOnError] => 1
[skipOnEmpty] => 1
[enableClientValidation] => 1
[isEmpty] =>
[when] =>
[whenClient] =>
[_events:yii\base\Component:private] => Array
(
)
[_behaviors:yii\base\Component:private] =>
)
[3] => yii\validators\StringValidator Object
(
[length] =>
[max] => 25
[min] =>
[message] => {attribute} must be a string.
[tooShort] =>
[tooLong] => {attribute} should contain at most {max, number} {max, plural, one{character} other{characters}}.
[notEqual] =>
[encoding] => utf-8
[attributes] => Array
(
[0] => employee_ID
[1] => auth_level
)
[on] => Array
(
)
[except] => Array
(
)
[skipOnError] => 1
[skipOnEmpty] => 1
[enableClientValidation] => 1
[isEmpty] =>
[when] =>
[whenClient] =>
[_events:yii\base\Component:private] => Array
(
)
[_behaviors:yii\base\Component:private] =>
)
[4] => yii\validators\NumberValidator Object
(
[integerOnly] => 1
[max] =>
[min] =>
[tooBig] =>
[tooSmall] =>
[integerPattern] => /^\s*[+-]?\d+\s*$/
[numberPattern] => /^\s*[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?\s*$/
[attributes] => Array
(
[0] => manager
)
[message] => {attribute} must be an integer.
[on] => Array
(
)
[except] => Array
(
)
[skipOnError] => 1
[skipOnEmpty] => 1
[enableClientValidation] => 1
[isEmpty] =>
[when] =>
[whenClient] =>
[_events:yii\base\Component:private] => Array
(
)
[_behaviors:yii\base\Component:private] =>
)
[5] => yii\validators\StringValidator Object
(
[length] =>
[max] => 50
[min] =>
[message] => {attribute} must be a string.
[tooShort] =>
[tooLong] => {attribute} should contain at most {max, number} {max, plural, one{character} other{characters}}.
[notEqual] =>
[encoding] => utf-8
[attributes] => Array
(
[0] => email
[1] => name
[2] => password
)
[on] => Array
(
)
[except] => Array
(
)
[skipOnError] => 1
[skipOnEmpty] => 1
[enableClientValidation] => 1
[isEmpty] =>
[when] =>
[whenClient] =>
[_events:yii\base\Component:private] => Array
(
)
[_behaviors:yii\base\Component:private] =>
)
[6] => yii\validators\CompareValidator Object
(
[compareAttribute] => password
[compareValue] =>
[type] => string
[operator] => ==
[message] => Passwords must match!
[attributes] => Array
(
ā¦