1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dacastro4\LaravelGmail\Traits; |
4
|
|
|
|
5
|
|
|
use Google_Service_Gmail; |
6
|
|
|
use Illuminate\Support\Facades\Storage; |
7
|
|
|
use Illuminate\Support\Arr; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Trait Configurable |
11
|
|
|
* @package Dacastro4\LaravelGmail\Traits |
12
|
|
|
*/ |
13
|
|
|
trait Configurable |
14
|
|
|
{ |
15
|
|
|
|
16
|
|
|
protected $additionalScopes = []; |
17
|
|
|
private $_config; |
18
|
|
|
|
19
|
|
|
public function __construct($config) |
20
|
|
|
{ |
21
|
|
|
$this->_config = $config; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public function config($string = null) |
25
|
|
|
{ |
26
|
|
|
$disk = Storage::disk('local'); |
27
|
|
|
$fileName = $this->getFileName(); |
28
|
|
|
$file = "gmail/tokens/$fileName.json"; |
29
|
|
|
$allowJsonEncrypt = $this->_config['gmail.allow_json_encrypt']; |
30
|
|
|
|
31
|
|
|
if ($disk->exists($file)) { |
32
|
|
|
if ($allowJsonEncrypt) { |
33
|
|
|
$config = json_decode(decrypt($disk->get($file)), true); |
34
|
|
|
} else { |
35
|
|
|
$config = json_decode($disk->get($file), true); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
if ($string) { |
39
|
|
|
if (isset($config[$string])) { |
40
|
|
|
return $config[$string]; |
41
|
|
|
} |
42
|
|
|
} else { |
43
|
|
|
return $config; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
return null; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
private function getFileName() |
52
|
|
|
{ |
53
|
|
|
if (property_exists(get_class($this), 'userId') && $this->userId) { |
54
|
|
|
$userId = $this->userId; |
55
|
|
|
} elseif (auth()->user()) { |
56
|
|
|
$userId = auth()->user()->id; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$credentialFilename = $this->_config['gmail.credentials_file_name']; |
60
|
|
|
$allowMultipleCredentials = $this->_config['gmail.allow_multiple_credentials']; |
61
|
|
|
|
62
|
|
|
if (isset($userId) && $allowMultipleCredentials) { |
63
|
|
|
return sprintf('%s-%s', $credentialFilename, $userId); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return $credentialFilename; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @return array |
71
|
|
|
*/ |
72
|
|
|
public function getConfigs() |
73
|
|
|
{ |
74
|
|
|
return [ |
75
|
|
|
'client_secret' => $this->_config['gmail.client_secret'], |
76
|
|
|
'client_id' => $this->_config['gmail.client_id'], |
77
|
|
|
'redirect_uri' => url($this->_config['gmail.redirect_url']), |
78
|
|
|
'state' => isset($this->_config['gmail.state']) ? $this->_config['gmail.state'] : null, |
79
|
|
|
]; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function setAdditionalScopes(array $scopes) |
83
|
|
|
{ |
84
|
|
|
$this->additionalScopes = $scopes; |
85
|
|
|
|
86
|
|
|
return $this; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
private function configApi() |
90
|
|
|
{ |
91
|
|
|
$type = $this->_config['gmail.access_type']; |
92
|
|
|
$approval_prompt = $this->_config['gmail.approval_prompt']; |
93
|
|
|
|
94
|
|
|
$this->setScopes($this->getUserScopes()); |
95
|
|
|
|
96
|
|
|
$this->setAccessType($type); |
97
|
|
|
|
98
|
|
|
$this->setApprovalPrompt($approval_prompt); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public abstract function setScopes($scopes); |
102
|
|
|
|
103
|
|
|
private function getUserScopes() |
104
|
|
|
{ |
105
|
|
|
return $this->mapScopes(); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
private function mapScopes() |
109
|
|
|
{ |
110
|
|
|
$scopes = array_merge($this->_config['gmail.scopes'] ?? [], $this->additionalScopes); |
111
|
|
|
$scopes = array_unique(array_filter($scopes)); |
112
|
|
|
$mappedScopes = []; |
113
|
|
|
|
114
|
|
|
if (!empty($scopes)) { |
115
|
|
|
foreach ($scopes as $scope) { |
116
|
|
|
$mappedScopes[] = $this->scopeMap($scope); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
return array_merge($mappedScopes, $this->_config['gmail.additional_scopes'] ?? []); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
private function scopeMap($scope) |
124
|
|
|
{ |
125
|
|
|
$scopes = [ |
126
|
|
|
'all' => Google_Service_Gmail::MAIL_GOOGLE_COM, |
127
|
|
|
'compose' => Google_Service_Gmail::GMAIL_COMPOSE, |
128
|
|
|
'insert' => Google_Service_Gmail::GMAIL_INSERT, |
129
|
|
|
'labels' => Google_Service_Gmail::GMAIL_LABELS, |
130
|
|
|
'metadata' => Google_Service_Gmail::GMAIL_METADATA, |
131
|
|
|
'modify' => Google_Service_Gmail::GMAIL_MODIFY, |
132
|
|
|
'readonly' => Google_Service_Gmail::GMAIL_READONLY, |
133
|
|
|
'send' => Google_Service_Gmail::GMAIL_SEND, |
134
|
|
|
'settings_basic' => Google_Service_Gmail::GMAIL_SETTINGS_BASIC, |
135
|
|
|
'settings_sharing' => Google_Service_Gmail::GMAIL_SETTINGS_SHARING, |
136
|
|
|
]; |
137
|
|
|
|
138
|
|
|
return Arr::get($scopes, $scope); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
public abstract function setAccessType($type); |
142
|
|
|
|
143
|
|
|
public abstract function setApprovalPrompt($approval); |
144
|
|
|
|
145
|
|
|
} |
146
|
|
|
|