Jump to content

how do I find out what groups a user belongs to?


Recommended Posts

I want to add my User to some general (site-wide) Groups (not Project Groups) via the API, similar to what I would do by hand.  But I haven't found many examples dealing with Groups.

What is the best way to find out all the groups a User belongs to?

It doesn't seem that I can find out from the User.  Do I need to go through all the Groups and check to see if my User is a member of that group?

Are there any examples of dealing with groups via the API?

I want to do two things:

1) Print the user name and the groups that user belongs to (not Project Groups, just general Groups).

% print_my_groups.py -u franz

User        Groups
----         ------
franz         Project Manager, User

2) Add the user to a specified general (site-wide) Group.

Link to comment
Share on other sites

Hi Fran, I'm travelling and don't have the ability to test it now but you have a memberships relation on the Group. Using this you should be able to do something like:

Group where memberships.user.username is "foo" and local is False

Then you should be able to create a new group membership as:

session.create('Membership',  {'group': group, 'user': user})

Again - haven't tested but something like that should work. Let me know otherwise and I will dig further.

Link to comment
Share on other sites

The first part worked great.

But I'm getting an error with creating the membership when I do the session commit.

I don't know what it doesn't like.  (What string value does it not like?  What is trying to set the relationship?)

user = session.query("User where username is 'fzand'").one()
group = session.query("Group where name is 'Test Subgroup' and local is False").one()
session.create('Membership', {'group': group, 'user': user})
<dynamic ftrack Membership object 140237857912208>

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/mill3d/users/fzand/dev/virtualenvs/ftrack/shadow/lib/python2.7/site-packages/ftrack_api/session.py", line 1165, in commit
    result = self._call(batch)
  File "/mill3d/users/fzand/dev/virtualenvs/ftrack/shadow/lib/python2.7/site-packages/ftrack_api/session.py", line 1544, in _call
    raise ftrack_api.exception.ServerError(error_message)
ftrack_api.exception.ServerError: Server reported error: ValueError(Cannot set relationship to string value.)

Am I missing something in the create?

Link to comment
Share on other sites

Yeah, I think something got messed up with my session.  Right before I left last night, I ran it through the py.test, and it worked fine. (!)

Now that I have one created, is this the correct syntax for deleting a user from the group?

session.delete('Membership', {'group': group, 'user': user})

Link to comment
Share on other sites

I found the answer (for those looking at this in the future):

group_name = 'Test Subgroup'
user_name = 'fzand'
membership = session.query(
                "Membership where group.name is '{0}' ".format(group_name) +
                "and user.username is '{0}'".format(user_name)


Link to comment
Share on other sites


This topic is now archived and is closed to further replies.

  • Create New...