|
| 1 | +<?php |
| 2 | + |
| 3 | + |
| 4 | +namespace USJ\GSuiteBundle\Manager; |
| 5 | + |
| 6 | + |
| 7 | +use Google_Service_Directory; |
| 8 | +use USJ\GSuiteBundle\Model\GroupManagerInterface; |
| 9 | +use USJ\GSuiteBundle\Model\UserInterface; |
| 10 | + |
| 11 | +class GroupManager implements GroupManagerInterface |
| 12 | +{ |
| 13 | + /** |
| 14 | + * @var Google_Service_Directory |
| 15 | + */ |
| 16 | + protected $directoryService; |
| 17 | + |
| 18 | + /** |
| 19 | + * @var \Google_Client |
| 20 | + */ |
| 21 | + private $client; |
| 22 | + |
| 23 | + public function __construct(\Google_Client $client) |
| 24 | + { |
| 25 | + $this->client = $client; |
| 26 | + } |
| 27 | + |
| 28 | + public function list($params): \Iterator |
| 29 | + { |
| 30 | + return $this->getDirectoryService()->groups->listGroups($params); |
| 31 | + } |
| 32 | + |
| 33 | + public function remove($groupEmail): void |
| 34 | + { |
| 35 | + $this->getDirectoryService()->groups->delete($groupEmail); |
| 36 | + } |
| 37 | + |
| 38 | + public function get($groupEmail): \Google_Service_Directory_Group |
| 39 | + { |
| 40 | + return $this->getDirectoryService()->groups->get($groupEmail); |
| 41 | + } |
| 42 | + |
| 43 | + public function listMembersInGroup($groupEmail): \Iterator |
| 44 | + { |
| 45 | + $nextPageToken = null; |
| 46 | + |
| 47 | + do { |
| 48 | + $result = $this->getDirectoryService()->members->listMembers( |
| 49 | + $groupEmail, |
| 50 | + $nextPageToken ? ['pageToken' => $nextPageToken] : [] |
| 51 | + ); |
| 52 | + |
| 53 | + foreach ($result as $item) { |
| 54 | + yield $item; |
| 55 | + } |
| 56 | + } while ($nextPageToken = $result->nextPageToken); |
| 57 | + } |
| 58 | + |
| 59 | + public function isUserInGroup($groupEmail, UserInterface $user): boolean |
| 60 | + { |
| 61 | + return (bool)$this->getDirectoryService()->members |
| 62 | + ->hasMember($groupEmail, $user->getGoogleEmail()) |
| 63 | + ->getIsMember(); |
| 64 | + } |
| 65 | + |
| 66 | + public function findGroupsByUser(UserInterface $user): \Iterator |
| 67 | + { |
| 68 | + return $this->getDirectoryService()->groups->listGroups(['userKey' => $user->getGoogleEmail()]); |
| 69 | + } |
| 70 | + |
| 71 | + public function addUserToGroup($groupEmail, UserInterface $user, $role = "MEMBER"): void |
| 72 | + { |
| 73 | + $member = new \Google_Service_Directory_Member(); |
| 74 | + $member->setEmail($user->getGoogleEmail()); |
| 75 | + $member->setRole($role); |
| 76 | + |
| 77 | + $this->getDirectoryService()->members->insert( |
| 78 | + $groupEmail, |
| 79 | + $member |
| 80 | + ); |
| 81 | + } |
| 82 | + |
| 83 | + public function removeUserFromGroup($groupEmail, UserInterface $user): void |
| 84 | + { |
| 85 | + $this->getDirectoryService()->members->delete($groupEmail, $user->getGoogleEmail()); |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * @return Google_Service_Directory |
| 90 | + */ |
| 91 | + private function getDirectoryService() |
| 92 | + { |
| 93 | + if (!$this->directoryService) { |
| 94 | + $this->directoryService = new Google_Service_Directory($this->client); |
| 95 | + } |
| 96 | + |
| 97 | + return $this->directoryService; |
| 98 | + } |
| 99 | +} |
0 commit comments