Skip to content
This repository was archived by the owner on Mar 31, 2022. It is now read-only.

Commit 7005e32

Browse files
committed
Initial commit
0 parents  commit 7005e32

16 files changed

+628
-0
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
vendor/
2+
.idea/

Client/DomainSpecificClient.php

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
4+
namespace USJ\USJGSuiteBundle\Client;
5+
6+
7+
class DomainSpecificClient
8+
{
9+
public function __construct()
10+
{
11+
12+
}
13+
14+
/**
15+
* client
16+
*/
17+
public function testSetSubject()
18+
{
19+
20+
}
21+
}

Client/Registry.php

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
4+
namespace USJ\USJGSuiteBundle\Client;
5+
6+
7+
class Registry
8+
{
9+
protected $clients;
10+
11+
public function addClient($name, DomainSpecificClient $client)
12+
{
13+
$this->clients[$name] = $client;
14+
}
15+
16+
public function get($name)
17+
{
18+
return $this->clients[$name];
19+
}
20+
}
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
4+
namespace USJ\USJGSuiteBundle\Client;
5+
6+
7+
class ServiceAccountClientFactory
8+
{
9+
10+
}

DependencyInjection/Configuration.php

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
4+
namespace USJ\GSuiteBundle\DependencyInjection;
5+
6+
7+
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
8+
use Symfony\Component\Config\Definition\ConfigurationInterface;
9+
10+
class Configuration implements ConfigurationInterface
11+
{
12+
13+
/**
14+
* {@inheritdoc}
15+
*/
16+
public function getConfigTreeBuilder()
17+
{
18+
$treeBuilder = new TreeBuilder();
19+
$rootNode = $treeBuilder
20+
->root('usj_gsuite', 'array')
21+
->children();
22+
23+
$rootNode
24+
->arrayNode('clients')
25+
->arrayPrototype()
26+
->children()
27+
->scalarNode('key')->isRequired()->end()
28+
->scalarNode('subject')->defaultValue('')->end()
29+
->arrayNode('scopes')
30+
->scalarPrototype()->end()
31+
->end()
32+
->end()
33+
->end()
34+
->end();
35+
36+
return $treeBuilder;
37+
}
38+
}
+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
3+
4+
namespace USJ\GSuiteBundle\DependencyInjection;
5+
6+
use Symfony\Component\DependencyInjection\ContainerBuilder;
7+
use Symfony\Component\DependencyInjection\Definition;
8+
use Symfony\Component\DependencyInjection\Reference;
9+
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
10+
use USJ\GSuiteBundle\Manager\UserManager;
11+
use USJ\USJGSuiteBundle\Model\UserManagerInterface;
12+
13+
class USJGSuiteExtension extends Extension
14+
{
15+
16+
/**
17+
* {@inheritdoc}
18+
*/
19+
public function load(array $configs, ContainerBuilder $container)
20+
{
21+
$configuration = new Configuration();
22+
$config = $this->processConfiguration($configuration, $configs);
23+
24+
$this->loadClientConfig($config, $container);
25+
}
26+
27+
protected function loadClientConfig(array $config, ContainerBuilder $container)
28+
{
29+
// create default client
30+
$defaultClient = $this->createClientDefinition($config['clients']['default']);
31+
32+
$container->addDefinitions([
33+
'usj_gsuite.client.default' => $defaultClient,
34+
]);
35+
36+
$container->addAliases([
37+
'usj_gsuite.client' => 'usj_gsuite.client.default',
38+
\Google_Client::class => 'usj_gsuite.client.default'
39+
]);
40+
41+
$userDefinition = $this->createUserDefinition('default', $container);
42+
43+
$container->addDefinitions(['usj_gsuite.default.user' => $userDefinition]);
44+
45+
$container->addAliases([
46+
'usj_gsuite.user' => 'usj_gsuite.default.user',
47+
UserManagerInterface::class => 'usj_gsuite.default.user'
48+
]);
49+
}
50+
51+
protected function createUserDefinition($clientId, ContainerBuilder $container)
52+
{
53+
$definition = new Definition(UserManager::class);
54+
$definition->setArgument(0, new Reference(sprintf('usj_gsuite.client.%s', $clientId)));
55+
$container->setDefinition(sprintf('usj_gsuite.%s.user', $clientId), $definition);
56+
57+
return $definition;
58+
}
59+
60+
private function createClientDefinition($config): Definition
61+
{
62+
$definition = new Definition(\Google_Client::class);
63+
64+
$definition->addMethodCall('setAuthConfig', [$config['key']]);
65+
$definition->addMethodCall('setSubject', [$config['subject']]);
66+
$definition->addMethodCall('setScopes', [$config['scopes']]);
67+
68+
return $definition;
69+
}
70+
71+
protected function getDefaultScopes()
72+
{
73+
return [
74+
'https://www.googleapis.com/auth/admin.directory.user',
75+
'https://www.googleapis.com/auth/admin.directory.group'
76+
];
77+
}
78+
}

Manager/UserManager.php

+122
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
<?php
2+
namespace USJ\GSuiteBundle\Manager;
3+
4+
use Google_Service_Directory;
5+
use Google_Service_Directory_User;
6+
use Psr\Log\LoggerAwareTrait;
7+
use USJ\USJGSuiteBundle\Client\DomainSpecificClient;
8+
use USJ\USJGSuiteBundle\Model\UserInterface;
9+
use USJ\USJGSuiteBundle\Model\UserManagerInterface;
10+
11+
class UserManager implements UserManagerInterface
12+
{
13+
use LoggerAwareTrait;
14+
15+
/**
16+
* @var Google_Service_Directory
17+
*/
18+
protected $directoryService;
19+
20+
/**
21+
* @var DomainSpecificClient
22+
*/
23+
private $client;
24+
25+
/**
26+
* @param \Google_Client $client
27+
*/
28+
public function __construct(\Google_Client $client)
29+
{
30+
$this->client = $client;
31+
}
32+
33+
public function setSubject(string $subject): UserManagerInterface
34+
{
35+
$this->client->setSubject($subject);
36+
37+
return $this;
38+
}
39+
40+
/**
41+
* {@inheritdoc}
42+
*/
43+
public function list($params): \Iterator
44+
{
45+
return $this->getDirectoryService()->users->listUsers($params);
46+
}
47+
48+
49+
/**
50+
* @param UserInterface $user
51+
*/
52+
public function insert(UserInterface $user): void
53+
{
54+
$googleUser = $this->transform($user);
55+
56+
$this->getDirectoryService()->users->insert($googleUser);
57+
}
58+
59+
/**
60+
* {@inheritdoc}
61+
*/
62+
public function delete(UserInterface $user): void
63+
{
64+
$this->getDirectoryService()->users->delete($user->getGoogleEmail());
65+
}
66+
67+
/**
68+
* {@inheritdoc}
69+
*/
70+
public function deleteById(string $id): void
71+
{
72+
$this->getDirectoryService()->users->delete($id);
73+
}
74+
75+
/**
76+
* {@inheritdoc}
77+
*/
78+
public function update(UserInterface $user): void
79+
{
80+
$this->getDirectoryService()->users->update($user->getGoogleEmail(), $this->transform($user));
81+
}
82+
83+
/**
84+
* {@inheritdoc}
85+
*/
86+
public function getById(string $id): Google_Service_Directory_User
87+
{
88+
$this->getDirectoryService()->users->get($id);
89+
}
90+
91+
/**
92+
* {@inheritdoc}
93+
*/
94+
public function has(string $id): boolean
95+
{
96+
return (bool) $this->getDirectoryService()->users->get($id);
97+
}
98+
99+
/**
100+
* @return Google_Service_Directory
101+
*/
102+
private function getDirectoryService()
103+
{
104+
if (!$this->directoryService) {
105+
$this->directoryService = new Google_Service_Directory($this->client);
106+
}
107+
108+
return $this->directoryService;
109+
}
110+
111+
private function transform(UserInterface $user): Google_Service_Directory_User
112+
{
113+
$googleUser = new Google_Service_Directory_User();
114+
115+
$googleUser->setPrimaryEmail($user->getGoogleEmail());
116+
$googleUser->setName($user->getGoogleDirectoryName());
117+
$googleUser->setHashFunction($user->getGooglePasswordHashFunc());
118+
$googleUser->setPassword($user->getGooglePassword());
119+
120+
return $googleUser;
121+
}
122+
}

Model/UserInterface.php

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
4+
namespace USJ\GSuiteBundle\Model;
5+
6+
7+
interface UserInterface
8+
{
9+
public function getGoogleDirectoryName(): \Google_Service_Directory_UserName;
10+
11+
public function getGoogleEmail(): string;
12+
13+
public function getGooglePasswordHashFunc(): string;
14+
15+
public function getGooglePassword(): string;
16+
}

Model/UserManagerInterface.php

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
4+
namespace USJ\USJGSuiteBundle\Model;
5+
6+
7+
interface UserManagerInterface
8+
{
9+
/**
10+
* @param string $id
11+
*
12+
* @return UserManagerInterface
13+
*/
14+
public function setSubject(string $id): UserManagerInterface;
15+
16+
/**
17+
* Add user to Google directory
18+
*
19+
* @param UserInterface $user
20+
*/
21+
public function insert(UserInterface $user): void;
22+
23+
/**
24+
* List users inside Google directory
25+
*
26+
* @param $params
27+
*
28+
* @return \Iterator
29+
*/
30+
public function list($params): \Iterator;
31+
32+
/**
33+
* @param string $id
34+
*
35+
* @return \Google_Service_Directory_User
36+
*/
37+
public function getById(string $id): \Google_Service_Directory_User;
38+
39+
/**
40+
* Update user with new data
41+
*
42+
* @param UserInterface $user
43+
*/
44+
public function update(UserInterface $user): void;
45+
46+
/**
47+
* Remove user from Google directory
48+
*
49+
* @param UserInterface $user
50+
*/
51+
public function delete(UserInterface $user): void;
52+
53+
/**
54+
* Remove user from Google directory by an identifier
55+
*
56+
* @param $id
57+
*/
58+
public function deleteById(string $id): void;
59+
60+
/**
61+
* Check if user with id exists
62+
*
63+
* @param string $id
64+
*
65+
* @return bool
66+
*/
67+
public function has(string $id): boolean;
68+
}

0 commit comments

Comments
 (0)