-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathGSuiteUserService.php
124 lines (105 loc) · 3.24 KB
/
GSuiteUserService.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php
namespace App\Services;
use Carbon\Carbon;
use Google_Client;
use Google_Exception;
use Google_Service_Directory;
use Google_Service_Directory_User;
use Google_Service_Directory_UserName;
use Google_Service_Exception;
class GSuiteUserService
{
public const USERLIMIT = 20;
protected $name;
protected $joinedOn;
protected $designation;
protected $users;
public function __construct()
{
$client = new Google_Client();
$client->useApplicationDefaultCredentials();
$client->setSubject(config('constants.gsuite.service-account-impersonate'));
$client->addScope([
Google_Service_Directory::ADMIN_DIRECTORY_USER,
Google_Service_Directory::ADMIN_DIRECTORY_USER_READONLY,
]);
$this->service = new Google_Service_Directory($client);
}
public function fetch($email)
{
$this->setUsers([$this->service->users->get($email)]);
}
public function fetchAll()
{
$optParams = [
'domain' => config('constants.gsuite.client-hd'),
];
$users = $this->service->users->listUsers($optParams)->getUsers();
$this->setUsers($users);
}
public function create($name, $email, $password, $params = [])
{
$user = new Google_Service_Directory_User;
$userName = new Google_Service_Directory_UserName;
$userName->setGivenName($name['firstName']);
$userName->setFamilyName($name['lastName']);
$user->setName($userName);
$user->setPrimaryEmail($email);
// Need to use a better hash function than MD5. Also, there should be a place
// where this hash type is stored. Can be a CONST.
$user->setHashFunction('MD5');
$user->setPassword(hash('md5', $password));
$user->setChangePasswordAtNextLogin(true);
if (isset($params['designation'])) {
$user->setOrganizations([
[
'title' => $params['designation'],
],
]);
}
try {
$gsuiteUser = $this->service->users->insert($user);
$this->setName($gsuiteUser->getName()->fullName);
$this->setJoinedOn(Carbon::parse($gsuiteUser->getCreationTime())->format(config('constants.date_format')));
if (isset($params['designation'])) {
$this->setDesignation($params['designation']);
}
} catch (Google_Exception $gioe) {
echo 'Error in connection: ' . $gioe->getMessage();
} catch (Google_Service_Exception $gse) {
echo $gse->getMessage();
}
}
public function setJoinedOn($joinedOn)
{
$this->joinedOn = $joinedOn;
}
public function getJoinedOn()
{
return $this->joinedOn;
}
public function setName($name)
{
$this->name = $name;
}
public function getName()
{
return $this->name;
}
public function setDesignation($designation)
{
$this->designation = $designation;
}
public function getDesignation()
{
return $this->designation;
}
public function setUsers(array $users)
{
$this->users = $users;
}
public function getUsers()
{
return $this->users;
}
}