Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
bb145f50
Commit
bb145f50
authored
Apr 30, 2014
by
Alexander Makarov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fixes #3179: removed duplicate validation rules from User model in advanced app,…
Fixes #3179: removed duplicate validation rules from User model in advanced app, removed User::create()
parent
9b69e2d1
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
19 additions
and
57 deletions
+19
-57
User.php
apps/advanced/common/models/User.php
+0
-44
SiteController.php
apps/advanced/frontend/controllers/SiteController.php
+1
-2
SignupForm.php
apps/advanced/frontend/models/SignupForm.php
+7
-1
security-authorization.md
docs/guide/security-authorization.md
+11
-10
No files found.
apps/advanced/common/models/User.php
View file @
bb145f50
...
...
@@ -29,26 +29,6 @@ class User extends ActiveRecord implements IdentityInterface
const
ROLE_USER
=
10
;
/**
* Creates a new user
*
* @param array $attributes the attributes given by field => value
* @return static|null the newly created model, or null on failure
*/
public
static
function
create
(
$attributes
)
{
/** @var User $user */
$user
=
new
static
();
$user
->
setAttributes
(
$attributes
);
$user
->
setPassword
(
$attributes
[
'password'
]);
$user
->
generateAuthKey
();
if
(
$user
->
save
())
{
return
$user
;
}
else
{
return
null
;
}
}
/**
* @inheritdoc
*/
public
function
behaviors
()
...
...
@@ -181,28 +161,4 @@ class User extends ActiveRecord implements IdentityInterface
{
$this
->
password_reset_token
=
null
;
}
/**
* @inheritdoc
*/
public
function
rules
()
{
return
[
[
'status'
,
'default'
,
'value'
=>
self
::
STATUS_ACTIVE
],
[
'status'
,
'in'
,
'range'
=>
[
self
::
STATUS_ACTIVE
,
self
::
STATUS_DELETED
]],
[
'role'
,
'default'
,
'value'
=>
self
::
ROLE_USER
],
[
'role'
,
'in'
,
'range'
=>
[
self
::
ROLE_USER
]],
[
'username'
,
'filter'
,
'filter'
=>
'trim'
],
[
'username'
,
'required'
],
[
'username'
,
'unique'
],
[
'username'
,
'string'
,
'min'
=>
2
,
'max'
=>
255
],
[
'email'
,
'filter'
,
'filter'
=>
'trim'
],
[
'email'
,
'required'
],
[
'email'
,
'email'
],
[
'email'
,
'unique'
],
];
}
}
apps/advanced/frontend/controllers/SiteController.php
View file @
bb145f50
...
...
@@ -120,8 +120,7 @@ class SiteController extends Controller
{
$model
=
new
SignupForm
();
if
(
$model
->
load
(
Yii
::
$app
->
request
->
post
()))
{
$user
=
$model
->
signup
();
if
(
$user
)
{
if
(
$user
=
$model
->
signup
())
{
if
(
Yii
::
$app
->
getUser
()
->
login
(
$user
))
{
return
$this
->
goHome
();
}
...
...
apps/advanced/frontend/models/SignupForm.php
View file @
bb145f50
...
...
@@ -43,7 +43,13 @@ class SignupForm extends Model
public
function
signup
()
{
if
(
$this
->
validate
())
{
return
User
::
create
(
$this
->
attributes
);
$user
=
new
User
();
$user
->
username
=
$this
->
username
;
$user
->
email
=
$this
->
email
;
$user
->
setPassword
(
$this
->
password
);
$user
->
generateAuthKey
();
$user
->
save
(
false
);
return
$user
;
}
return
null
;
...
...
docs/guide/security-authorization.md
View file @
bb145f50
...
...
@@ -268,18 +268,19 @@ After executing the command we'll get the following hierarchy:
Author can create post, admin can update post and do everything author can.
If your application allows user signup you need to assign roles to these new users once. For example, in order for all
signed up users to become authors you in advanced application template you need to modify
`
common\models\User::create
()`
signed up users to become authors you in advanced application template you need to modify
`
frontend\models\SignupForm::signup
()`
as follows:
```
php
public
static
function
create
(
$attributes
)
public
function
signup
(
)
{
/** @var User $user */
$user
=
new
static
();
$user
->
setAttributes
(
$attributes
);
$user
->
setPassword
(
$attributes
[
'password'
]);
$user
->
generateAuthKey
();
if
(
$user
->
save
())
{
if
(
$this
->
validate
())
{
$user
=
new
User
();
$user
->
username
=
$this
->
username
;
$user
->
email
=
$this
->
email
;
$user
->
setPassword
(
$this
->
password
);
$user
->
generateAuthKey
();
$user
->
save
(
false
);
// the following three lines were added:
$auth
=
Yii
::
$app
->
authManager
;
...
...
@@ -287,9 +288,9 @@ public static function create($attributes)
$auth
->
assign
(
$authorRole
,
$user
->
getId
());
return
$user
;
}
else
{
return
null
;
}
return
null
;
}
```
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment