1

I have a Google workspace account, and I am creating a php panel to manage the team drives, I can already create the drive and add users, the problem is when I have to remove the user from the team drive, I am not able to.

When I create the drive my user is already in the drive as an administrator what I want to do is add two users and remove mine, from the Google drive panel this is possible, but I am not able to remove my user with the api.

//Create the team drive

    $requestId = $this->user->id.time();
        $client = new ApiGoogleDrive(1000);
        try {
            $teamDrive = $client->createDrive($requestId , $data["name"]);
            //var_dump($teamDrive);
            //saves the data of the created drive in the database
            $myDrive->id_user = $this->user->id;
            $myDrive->id_drive = $teamDrive->getId();
            $myDrive->request_id = $requestId;
            $myDrive->email = $data["email"];
            $myDrive->name = $data["name"];

            if (!$myDrive->save()){
                $json["message"] = $this->message->error("Oops! there was a failure creating the drive.")->render();
                echo json_encode($json);
                return;
            }
        }catch (Exception $exception){
             var_dump($exception);
             return;
        }

try {
            /**
             * add user
             * reference:https://stackoverflow.com/a/55720522/18215077 | https://developers.google.com/drive/api/v3/reference/permissions
             * Role: organizer, fileOrganizer, writer, commenter, reader
             */
            $service = $client->ServiceDrive();
            $postBody = new Google_Service_Drive_Permission();
            $postBody->setKind('drive#permission');
            $postBody->setEmailAddress($data["email"]);
            $postBody->setType("user");
            //$postBody->setRole("organizer"); // Administrador
            $postBody->setRole("fileOrganizer");
            $optParams = [
                'supportsTeamDrives' => true
            ];


            $drive_id = $teamDrive->getId();
            try {
                $service->permissions->create($drive_id, $postBody, $optParams);
                //$service->permissions->create($drive_id, $apiPostBody, $optParams);
            } catch (Google_Service_Exception $e) {
                //return abort($e->getCode());
                var_dump($e->getCode());
            }

        }catch (Exception $exception){
            var_dump($exception);
        }

But I don't even know where to start when it comes to removing the user from the team drive, could someone show me some example code.

Kelison Bessa
  • 45
  • 1
  • 6

1 Answers1

1

Shared drive permissions can be managed in the same way like any other file permissions

  • Use the method Permissions: list specifying th eid of the shared Drive as the fileId parameter and set supportsAllDrives to true and fields to *
  • This will return you a Permissions Resource contianing the associated parameters including the permission id and the emailAddress of the user
  • Find the permission where the user (and role) matches your search criteria and use the respective permission id as permissionId for the method Permissions: delete
  • This will allow you to delete the respective permission - and thus, remove the user from the Team Drive (once again, you need to set supportsAllDrives to true)

enter image description here

ziganotschka
  • 25,866
  • 2
  • 16
  • 33