Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
social_auth
Manage
Activity
Members
Labels
Plan
Wiki
Custom issue tracker
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Model registry
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
project
social_auth
Commits
fd8ee6f7
Commit
fd8ee6f7
authored
5 years ago
by
David Pashaev
Committed by
Getulio Valentin Sánchez
5 years ago
Browse files
Options
Downloads
Patches
Plain Diff
Issue
#3013484
by gvso, davps, manuel.adan, cbeier: Serialize additional data in entity
parent
660a5bff
Branches
2486651-donate
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
src/AuthManager/OAuth2Manager.php
+4
-4
4 additions, 4 deletions
src/AuthManager/OAuth2Manager.php
src/Entity/SocialAuth.php
+17
-18
17 additions, 18 deletions
src/Entity/SocialAuth.php
tests/src/Kernel/SocialAuthEntityTest.php
+99
-0
99 additions, 0 deletions
tests/src/Kernel/SocialAuthEntityTest.php
with
120 additions
and
22 deletions
src/AuthManager/OAuth2Manager.php
+
4
−
4
View file @
fd8ee6f7
...
...
@@ -14,14 +14,14 @@ abstract class OAuth2Manager extends BaseOAuth2Manager implements OAuth2ManagerI
/**
* The scopes to be requested.
*
* @var string
* @var string
|null
*/
protected
$scopes
;
/**
* The end points to be requested.
*
* @var string
* @var string
|null
*/
protected
$endPoints
;
...
...
@@ -60,7 +60,7 @@ public function getExtraDetails($method = 'GET', $domain = NULL) {
* {@inheritdoc}
*/
public
function
getScopes
()
{
if
(
$this
->
scopes
===
FALSE
)
{
if
(
$this
->
scopes
===
NULL
)
{
$this
->
scopes
=
$this
->
settings
->
get
(
'scopes'
);
}
...
...
@@ -71,7 +71,7 @@ public function getScopes() {
* {@inheritdoc}
*/
public
function
getEndPoints
()
{
if
(
$this
->
endPoints
===
FALSE
)
{
if
(
$this
->
endPoints
===
NULL
)
{
$this
->
endPoints
=
$this
->
settings
->
get
(
'endpoints'
);
}
...
...
This diff is collapsed.
Click to expand it.
src/Entity/SocialAuth.php
+
17
−
18
View file @
fd8ee6f7
...
...
@@ -2,11 +2,11 @@
namespace
Drupal\social_auth\Entity
;
use
Drupal\Core\Entity\EntityStorageInterface
;
use
Drupal\Core\Field\BaseFieldDefinition
;
use
Drupal\Core\Entity\EntityTypeInterface
;
use
Drupal\Core\Entity\ContentEntityInterface
;
use
Drupal\social_api
\Entity\SocialApi
;
use
function
GuzzleHttp\json_encode
;
/**
* Defines the Social Auth entity.
...
...
@@ -31,14 +31,13 @@ class SocialAuth extends SocialApi implements ContentEntityInterface {
/**
* {@inheritdoc}
*/
public
static
function
create
(
array
$values
=
[])
{
public
static
function
preCreate
(
EntityStorageInterface
$storage
,
array
&
$values
)
{
$additional_data
=
$values
[
'additional_data'
]
??
NULL
;
if
(
$additional_data
)
{
$values
[
'additional_data'
]
=
static
::
serializeData
(
$additional_data
);
$values
[
'additional_data'
]
=
static
::
encode
(
$additional_data
);
}
return
parent
::
c
reate
(
$values
);
return
parent
::
preC
reate
(
$storage
,
$values
);
}
/**
...
...
@@ -48,20 +47,20 @@ public static function create(array $values = []) {
* The user id.
*/
public
function
getUserId
()
{
return
$this
->
get
(
'user_id'
)
->
value
;
return
$this
->
get
(
'user_id'
)
->
target_id
;
}
/**
* Sets the additional data.
*
* @param array $data
* The
serialized
additional data.
* The additional data.
*
* @return \Drupal\social_auth\Entity\SocialAuth
* Drupal Social Auth Entity.
*/
public
function
setAdditionalData
(
array
$data
)
{
$this
->
set
(
'additional_data'
,
$this
->
serializeData
(
$data
));
$this
->
set
(
'additional_data'
,
$this
->
encode
(
$data
));
return
$this
;
}
...
...
@@ -70,12 +69,12 @@ public function setAdditionalData(array $data) {
* Returns the serialized additional data.
*
* @return array
* The
deserialized
additional data.
* The additional data.
*/
public
function
getAdditionalData
()
{
$data
=
$this
->
get
(
'additional_data'
)
->
value
;
return
$this
->
deserializeData
(
$data
)
;
return
$this
->
hasField
(
'additional_data'
)
&&
!
$this
->
get
(
'additional_data'
)
->
isEmpty
()
?
$this
->
decode
(
$this
->
get
(
'additional_data'
)
->
value
)
:
[]
;
}
/**
...
...
@@ -182,7 +181,7 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
}
/**
*
Serializ
es array to store in the additional data field.
*
Encod
es array to store in the additional data field.
*
* @param array $data
* The additional data.
...
...
@@ -190,20 +189,20 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
* @return string
* The serialized data.
*/
protected
static
function
serializeData
(
array
$data
)
{
protected
static
function
encode
(
array
$data
)
{
return
json_encode
(
$data
);
}
/**
* De
serializ
es string stored in the additional data field.
* De
cod
es string stored in the additional data field.
*
* @param string $data
* The
serializ
ed additional data.
* The
encod
ed additional data.
*
* @return array
* The de
serializ
ed data.
* The de
cod
ed data.
*/
protected
function
de
serializeData
(
string
$data
)
{
protected
function
de
code
(
string
$data
)
{
return
json_decode
(
$data
,
TRUE
);
}
...
...
This diff is collapsed.
Click to expand it.
tests/src/Kernel/SocialAuthEntityTest.php
0 → 100644
+
99
−
0
View file @
fd8ee6f7
<?php
namespace
Drupal\Tests\social_auth\Kernel
;
use
Drupal\KernelTests\Core\Entity\EntityKernelTestBase
;
use
Drupal\social_auth
\Entity\SocialAuth
;
/**
* Tests social_auth entity.
*
* @group social_auth
*/
class
SocialAuthEntityTest
extends
EntityKernelTestBase
{
/**
* The social_auth entity.
*
* @var \Drupal\social_auth\Entity\SocialAuth
*/
protected
$entity
;
/**
* The entity storage.
*
* @var \Drupal\Core\Entity\EntityStorageInterface
*/
protected
$entityStorage
;
/**
* The entity values to creation.
*
* @var array
*/
protected
$values
=
[];
/**
* Modules to enable.
*
* @var array
*/
public
static
$modules
=
[
'social_api'
,
'social_auth'
];
/**
* {@inheritdoc}
*/
protected
function
setUp
()
{
parent
::
setUp
();
$this
->
installEntitySchema
(
'social_auth'
);
$this
->
entityStorage
=
$this
->
entityTypeManager
->
getStorage
(
'social_auth'
);
$user
=
$this
->
drupalCreateUser
();
$this
->
values
=
[
'user_id'
=>
$user
->
id
(),
'plugin_id'
=>
'social_auth_provider_test'
,
'provider_user_id'
=>
'provider_id_test'
,
'additional_data'
=>
[
'foo'
=>
'bar'
],
'token'
=>
'token_test'
,
];
$this
->
entity
=
$this
->
entityStorage
->
create
(
$this
->
values
);
$this
->
entity
->
save
();
}
/**
* Tests entity creation.
*/
public
function
testEntityCreation
()
{
$entity1
=
SocialAuth
::
create
(
$this
->
values
);
$entity2
=
$this
->
entityStorage
->
create
(
$this
->
values
);
$values1
=
$entity1
->
toArray
();
$values2
=
$entity2
->
toArray
();
unset
(
$values1
[
'uuid'
],
$values2
[
'uuid'
],
$values1
[
'token'
],
$values2
[
'token'
]);
self
::
assertEquals
(
$values1
,
$values2
);
}
/**
* Tests getter for user_id field.
*/
public
function
testUserId
()
{
self
::
assertEquals
(
$this
->
values
[
'user_id'
],
$this
->
entity
->
getUserId
());
}
/**
* Tests getter/setter for additional_data field.
*/
public
function
testAdditionalData
()
{
self
::
assertEquals
(
$this
->
values
[
'additional_data'
],
$this
->
entity
->
getAdditionalData
());
$new_value
=
[];
$this
->
entity
->
setAdditionalData
(
$new_value
);
$this
->
entity
->
save
();
self
::
assertEquals
(
$new_value
,
$this
->
entity
->
getAdditionalData
());
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment