Skip to content

validate_shotgrid_user

ValidateShotgridUser

Bases: ContextPlugin

Check if user is valid and have access to the project.

Source code in client/ayon_shotgrid/plugins/publish/validate_shotgrid_user.py
 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
class ValidateShotgridUser(pyblish.api.ContextPlugin):
    """
    Check if user is valid and have access to the project.
    """
    label = "Validate Shotgrid User"
    order = ValidateContentsOrder

    def process(self, context):
        sg_session = context.data.get("shotgridSession")
        user_login = context.data.get("shotgridUser")
        sg_project = context.data.get("shotgridProject")
        project_name = context.data["projectEntity"]["name"]

        if not (user_login and sg_session and sg_project):
            raise PublishValidationError("Missing Shotgrid Credentials")

        self.log.info("Login ShotGrid set in AYON is {}".format(user_login))
        self.log.info("Current ShotGrid Project is {}".format(sg_project))

        sg_user = sg_session.find_one(
            "HumanUser",
            [["login", "is", user_login]],
            ["projects", "permission_rule_set"],
        )

        sg_user_has_permission = False

        if sg_user:
            sg_user_has_permission = sg_user["permission_rule_set"]["name"] == "Admin"

        # It's not an admin, but it might still have permissions
        if not sg_user_has_permission:
            for project in sg_user["projects"]:
                if project["name"] == project_name:
                    sg_user_has_permission = True
                    break

        if not sg_user_has_permission:
            raise PublishValidationError(
                "Login {0} doesn't have access to the project {1} <{2}>".format(
                    user_login, project_name, sg_project
                )
            )

        self.log.info(
            "Login {0} has access to the project {1} <{2}>".format(
                user_login, project_name, sg_project
            )
        )