|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace MadWeb\SocialAuth\Traits; |
|
4
|
|
|
|
|
5
|
|
|
use DateInterval; |
|
6
|
|
|
use Laravel\Socialite\Contracts\User; |
|
7
|
|
|
use MadWeb\SocialAuth\Models\SocialProvider; |
|
8
|
|
|
|
|
9
|
|
|
trait UserSocialite |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* User socials relationship. |
|
13
|
|
|
* |
|
14
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
|
15
|
|
|
*/ |
|
16
|
42 |
|
public function socials() |
|
17
|
|
|
{ |
|
18
|
42 |
|
$social_pivot_table_name = config('social-auth.table_names.user_has_social_provider'); |
|
19
|
|
|
|
|
20
|
42 |
|
return $this->belongsToMany(SocialProvider::class, $social_pivot_table_name); |
|
|
|
|
|
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Check social network is attached to user. |
|
25
|
|
|
* |
|
26
|
|
|
* @param $slug |
|
27
|
|
|
* @return mixed |
|
28
|
|
|
*/ |
|
29
|
9 |
|
public function isAttached(string $slug): bool |
|
30
|
|
|
{ |
|
31
|
9 |
|
return $this->socials()->where(['slug' => $slug])->exists(); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Attach social network provider to the user. |
|
36
|
|
|
* |
|
37
|
|
|
* @param SocialProvider $social |
|
38
|
|
|
* @param string $socialId |
|
39
|
|
|
* @param string $token |
|
40
|
|
|
* @param int $expiresIn |
|
41
|
|
|
*/ |
|
42
|
24 |
|
public function attachSocial($social, string $socialId, string $token, int $expiresIn = null) |
|
43
|
|
|
{ |
|
44
|
24 |
|
$data = ['social_id' => $socialId, 'token' => $token]; |
|
45
|
|
|
|
|
46
|
24 |
|
$expiresIn = $expiresIn |
|
47
|
6 |
|
? date_create('now') |
|
48
|
6 |
|
->add(DateInterval::createFromDateString($expiresIn.' seconds')) |
|
49
|
6 |
|
->format($this->getDateFormat()) |
|
|
|
|
|
|
50
|
24 |
|
: false; |
|
51
|
|
|
|
|
52
|
24 |
|
if ($expiresIn) { |
|
53
|
6 |
|
$data['expires_in'] = $expiresIn; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
24 |
|
$this->socials()->attach($social, $data); |
|
57
|
24 |
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @param User $socialUser |
|
61
|
|
|
* @return array |
|
62
|
|
|
*/ |
|
63
|
6 |
|
public function mapSocialData(User $socialUser) |
|
64
|
|
|
{ |
|
65
|
6 |
|
$raw = $socialUser->getRaw(); |
|
66
|
6 |
|
$name = $socialUser->getName() ?? $socialUser->getNickname(); |
|
67
|
6 |
|
$name = $name ?? $socialUser->getEmail(); |
|
68
|
|
|
|
|
69
|
|
|
$result = [ |
|
70
|
6 |
|
$this->getEmailField() => $socialUser->getEmail(), |
|
71
|
6 |
|
'name' => $name, |
|
72
|
6 |
|
'verified' => $raw['verified'] ?? true, |
|
73
|
6 |
|
'avatar' => $socialUser->getAvatar(), |
|
74
|
|
|
]; |
|
75
|
|
|
|
|
76
|
6 |
|
return $result; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Get model email field name. |
|
81
|
|
|
* |
|
82
|
|
|
* @return string |
|
83
|
|
|
*/ |
|
84
|
21 |
|
public function getEmailField(): string |
|
85
|
|
|
{ |
|
86
|
21 |
|
return 'email'; |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idableprovides a methodequalsIdthat in turn relies on the methodgetId(). If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()as an abstract method to the trait will make sure it is available.