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
ba95e1dd
Commit
ba95e1dd
authored
Dec 23, 2013
by
Paul Klimov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Normalization of user attributes added to Auth Client.
parent
f06b5ab1
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
106 additions
and
34 deletions
+106
-34
ClientTrait.php
extensions/yii/authclient/ClientTrait.php
+72
-14
ClientTraitTest.php
tests/unit/extensions/authclient/ClientTraitTest.php
+34
-20
No files found.
extensions/yii/authclient/ClientTrait.php
View file @
ba95e1dd
...
@@ -12,27 +12,35 @@ use yii\base\NotSupportedException;
...
@@ -12,27 +12,35 @@ use yii\base\NotSupportedException;
use
yii\helpers\StringHelper
;
use
yii\helpers\StringHelper
;
/**
/**
*
Class ProviderTrait
*
ProviderTrait can be used to satisfy [[ClientInterface]] interface.
*
*
* @see ClientInterface
* @see ClientInterface
*
*
* @property string $id auth service id.
* @property string $name auth service name.
* @property string $title auth service title.
* @property array $userAttributes authenticated user attributes.
* @property array $normalizeUserAttributeMap map used to normalize user attributes fetched from
* external auth service in format: rawAttributeName => normalizedAttributeName.
* @property array $viewOptions view options in format: optionName => optionValue.
*
* @author Paul Klimov <klimov.paul@gmail.com>
* @author Paul Klimov <klimov.paul@gmail.com>
* @since 2.0
* @since 2.0
*/
*/
trait
ClientTrait
trait
ClientTrait
{
{
/**
/**
* @var string service id.
* @var string
auth
service id.
* This value mainly used as HTTP request parameter.
* This value mainly used as HTTP request parameter.
*/
*/
private
$_id
;
private
$_id
;
/**
/**
* @var string
service uniqu
e name.
* @var string
auth servic
e name.
* This value may be used in database records, CSS files and so on.
* This value may be used in database records, CSS files and so on.
*/
*/
private
$_name
;
private
$_name
;
/**
/**
* @var string service title to display in views.
* @var string
auth
service title to display in views.
*/
*/
private
$_title
;
private
$_title
;
/**
/**
...
@@ -40,6 +48,11 @@ trait ClientTrait
...
@@ -40,6 +48,11 @@ trait ClientTrait
*/
*/
private
$_userAttributes
;
private
$_userAttributes
;
/**
/**
* @var array map used to normalize user attributes fetched from external auth service
* in format: rawAttributeName => normalizedAttributeName
*/
private
$_normalizeUserAttributeMap
;
/**
* @var array view options in format: optionName => optionValue
* @var array view options in format: optionName => optionValue
*/
*/
private
$_viewOptions
;
private
$_viewOptions
;
...
@@ -64,6 +77,14 @@ trait ClientTrait
...
@@ -64,6 +77,14 @@ trait ClientTrait
}
}
/**
/**
* @param string $name service name.
*/
public
function
setName
(
$name
)
{
$this
->
_name
=
$name
;
}
/**
* @return string service name.
* @return string service name.
*/
*/
public
function
getName
()
public
function
getName
()
...
@@ -75,11 +96,11 @@ trait ClientTrait
...
@@ -75,11 +96,11 @@ trait ClientTrait
}
}
/**
/**
* @param string $
name service nam
e.
* @param string $
title service titl
e.
*/
*/
public
function
set
Name
(
$nam
e
)
public
function
set
Title
(
$titl
e
)
{
{
$this
->
_
name
=
$nam
e
;
$this
->
_
title
=
$titl
e
;
}
}
/**
/**
...
@@ -94,11 +115,11 @@ trait ClientTrait
...
@@ -94,11 +115,11 @@ trait ClientTrait
}
}
/**
/**
* @param
string $title service title.
* @param
array $userAttributes list of user attributes
*/
*/
public
function
set
Title
(
$title
)
public
function
set
UserAttributes
(
$userAttributes
)
{
{
$this
->
_
title
=
$title
;
$this
->
_
userAttributes
=
$this
->
normalizeUserAttributes
(
$userAttributes
)
;
}
}
/**
/**
...
@@ -107,17 +128,28 @@ trait ClientTrait
...
@@ -107,17 +128,28 @@ trait ClientTrait
public
function
getUserAttributes
()
public
function
getUserAttributes
()
{
{
if
(
$this
->
_userAttributes
===
null
)
{
if
(
$this
->
_userAttributes
===
null
)
{
$this
->
_userAttributes
=
$this
->
initUserAttributes
(
);
$this
->
_userAttributes
=
$this
->
normalizeUserAttributes
(
$this
->
initUserAttributes
()
);
}
}
return
$this
->
_userAttributes
;
return
$this
->
_userAttributes
;
}
}
/**
/**
* @param array $
userAttributes list of user attributes
* @param array $
normalizeUserAttributeMap normalize user attribute map.
*/
*/
public
function
set
UserAttributes
(
$userAttributes
)
public
function
set
NormalizeUserAttributeMap
(
$normalizeUserAttributeMap
)
{
{
$this
->
_userAttributes
=
$userAttributes
;
$this
->
_normalizeUserAttributeMap
=
$normalizeUserAttributeMap
;
}
/**
* @return array normalize user attribute map.
*/
public
function
getNormalizeUserAttributeMap
()
{
if
(
$this
->
_normalizeUserAttributeMap
===
null
)
{
$this
->
_normalizeUserAttributeMap
=
$this
->
defaultNormalizeUserAttributeMap
();
}
return
$this
->
_normalizeUserAttributeMap
;
}
}
/**
/**
...
@@ -167,6 +199,16 @@ trait ClientTrait
...
@@ -167,6 +199,16 @@ trait ClientTrait
}
}
/**
/**
* Returns the default [[normalizeUserAttributeMap]] value.
* Particular client may override this method in order to provide specific default map.
* @return array normalize attribute map.
*/
public
function
defaultNormalizeUserAttributeMap
()
{
return
[];
}
/**
* Returns the default [[viewOptions]] value.
* Returns the default [[viewOptions]] value.
* Particular client may override this method in order to provide specific default view options.
* Particular client may override this method in order to provide specific default view options.
* @return array list of default [[viewOptions]]
* @return array list of default [[viewOptions]]
...
@@ -175,4 +217,19 @@ trait ClientTrait
...
@@ -175,4 +217,19 @@ trait ClientTrait
{
{
return
[];
return
[];
}
}
/**
* Normalize given user attributes according to {@link normalizeUserAttributeMap}.
* @param array $attributes raw attributes.
* @return array normalized attributes.
*/
protected
function
normalizeUserAttributes
(
$attributes
)
{
foreach
(
$this
->
getNormalizeUserAttributeMap
()
as
$normalizedName
=>
$actualName
)
{
if
(
array_key_exists
(
$actualName
,
$attributes
))
{
$attributes
[
$normalizedName
]
=
$attributes
[
$actualName
];
}
}
return
$attributes
;
}
}
}
\ No newline at end of file
tests/unit/extensions/authclient/ClientTraitTest.php
View file @
ba95e1dd
...
@@ -8,22 +8,6 @@ use yii\base\Object;
...
@@ -8,22 +8,6 @@ use yii\base\Object;
class
ClientTraitTest
extends
TestCase
class
ClientTraitTest
extends
TestCase
{
{
protected
function
setUp
()
{
$config
=
[
'components'
=>
[
'user'
=>
[
'identityClass'
=>
'\yii\web\IdentityInterface'
],
'request'
=>
[
'hostInfo'
=>
'http://testdomain.com'
,
'scriptUrl'
=>
'/index.php'
,
],
]
];
$this
->
mockApplication
(
$config
,
'\yii\web\Application'
);
}
public
function
testSetGet
()
public
function
testSetGet
()
{
{
$client
=
new
Client
();
$client
=
new
Client
();
...
@@ -47,6 +31,13 @@ class ClientTraitTest extends TestCase
...
@@ -47,6 +31,13 @@ class ClientTraitTest extends TestCase
$client
->
setUserAttributes
(
$userAttributes
);
$client
->
setUserAttributes
(
$userAttributes
);
$this
->
assertEquals
(
$userAttributes
,
$client
->
getUserAttributes
(),
'Unable to setup user attributes!'
);
$this
->
assertEquals
(
$userAttributes
,
$client
->
getUserAttributes
(),
'Unable to setup user attributes!'
);
$normalizeUserAttributeMap
=
[
'name'
=>
'some/name'
,
'email'
=>
'some/email'
,
];
$client
->
setNormalizeUserAttributeMap
(
$normalizeUserAttributeMap
);
$this
->
assertEquals
(
$normalizeUserAttributeMap
,
$client
->
getNormalizeUserAttributeMap
(),
'Unable to setup normalize user attribute map!'
);
$viewOptions
=
[
$viewOptions
=
[
'option1'
=>
'value1'
,
'option1'
=>
'value1'
,
'option2'
=>
'value2'
,
'option2'
=>
'value2'
,
...
@@ -57,11 +48,34 @@ class ClientTraitTest extends TestCase
...
@@ -57,11 +48,34 @@ class ClientTraitTest extends TestCase
public
function
testGetDefaults
()
public
function
testGetDefaults
()
{
{
$provider
=
new
Client
();
$client
=
new
Client
();
$this
->
assertNotEmpty
(
$client
->
getName
(),
'Unable to get default name!'
);
$this
->
assertNotEmpty
(
$client
->
getTitle
(),
'Unable to get default title!'
);
$this
->
assertNotNull
(
$client
->
getViewOptions
(),
'Unable to get default view options!'
);
$this
->
assertNotNull
(
$client
->
getNormalizeUserAttributeMap
(),
'Unable to get default normalize user attribute map!'
);
}
/**
* @depends testSetGet
*/
public
function
testNormalizeUserAttributes
()
{
$client
=
new
Client
();
$this
->
assertNotEmpty
(
$provider
->
getName
(),
'Unable to get default name!'
);
$normalizeUserAttributeMap
=
[
$this
->
assertNotEmpty
(
$provider
->
getTitle
(),
'Unable to get default title!'
);
'raw/name'
=>
'name'
,
$this
->
assertNotNull
(
$provider
->
getViewOptions
(),
'Unable to get default view options!'
);
'raw/email'
=>
'email'
,
];
$client
->
setNormalizeUserAttributeMap
(
$normalizeUserAttributeMap
);
$rawUserAttributes
=
[
'raw/name'
=>
'name value'
,
'raw/email'
=>
'email value'
,
];
$client
->
setUserAttributes
(
$rawUserAttributes
);
$normalizedUserAttributes
=
$client
->
getUserAttributes
();
$expectedNormalizedUserAttributes
=
array_combine
(
array_keys
(
$normalizeUserAttributeMap
),
array_values
(
$rawUserAttributes
));
$this
->
assertEquals
(
$expectedNormalizedUserAttributes
,
$normalizedUserAttributes
);
}
}
}
}
...
...
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