1 # Copyright 2011 GRNET S.A. All rights reserved.
3 # Redistribution and use in source and binary forms, with or
4 # without modification, are permitted provided that the following
7 # 1. Redistributions of source code must retain the above
8 # copyright notice, this list of conditions and the following
11 # 2. Redistributions in binary form must reproduce the above
12 # copyright notice, this list of conditions and the following
13 # disclaimer in the documentation and/or other materials
14 # provided with the distribution.
16 # THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 # USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 # POSSIBILITY OF SUCH DAMAGE.
29 # The views and conclusions contained in the software and
30 # documentation are those of the authors and should not be
31 # interpreted as representing official policies, either expressed
32 # or implied, of GRNET S.A.
34 from collections import defaultdict
35 from sqlalchemy import Table, Column, String, MetaData
36 from sqlalchemy.sql import select, and_
37 from sqlalchemy.schema import Index
38 from dbworker import DBWorker
40 class Groups(DBWorker):
41 """Groups are named collections of members, belonging to an owner."""
43 def __init__(self, **params):
44 DBWorker.__init__(self, **params)
47 columns.append(Column('owner', String(255), primary_key=True))
48 columns.append(Column('name', String(255), primary_key=True))
49 columns.append(Column('member', String(255), primary_key=True))
50 self.groups = Table('groups', metadata, *columns, mysql_engine='InnoDB', mysql_charset='utf8')
52 # place an index on member
53 Index('idx_groups_member', self.groups.c.member)
55 metadata.create_all(self.engine)
57 def group_names(self, owner):
58 """List all group names belonging to owner."""
60 s = select([self.groups.c.name],
61 self.groups.c.owner==owner).distinct()
62 r = self.conn.execute(s)
63 l = [row[0] for row in r.fetchall()]
67 def group_dict(self, owner):
68 """Return a dict mapping group names to member lists for owner."""
70 s = select([self.groups.c.name, self.groups.c.member],
71 self.groups.c.owner==owner)
72 r = self.conn.execute(s)
74 for group, member in r.fetchall():
75 d[group].append(member)
79 def group_add(self, owner, group, member):
80 """Add a member to a group."""
82 s = self.groups.select()
83 s = s.where(self.groups.c.owner == owner)
84 s = s.where(self.groups.c.name == group)
85 s = s.where(self.groups.c.member == member)
86 r = self.conn.execute(s)
90 s = self.groups.insert()
91 r = self.conn.execute(s, owner=owner, name=group, member=member)
94 def group_addmany(self, owner, group, members):
95 """Add members to a group."""
97 #TODO: more efficient way to do it
98 for member in members:
99 self.group_add(owner, group, member)
101 def group_remove(self, owner, group, member):
102 """Remove a member from a group."""
104 s = self.groups.delete().where(and_(self.groups.c.owner==owner,
105 self.groups.c.name==group,
106 self.groups.c.member==member))
107 r = self.conn.execute(s)
110 def group_delete(self, owner, group):
111 """Delete a group."""
113 s = self.groups.delete().where(and_(self.groups.c.owner==owner,
114 self.groups.c.name==group))
115 r = self.conn.execute(s)
118 def group_destroy(self, owner):
119 """Delete all groups belonging to owner."""
121 s = self.groups.delete().where(self.groups.c.owner==owner)
122 r = self.conn.execute(s)
125 def group_members(self, owner, group):
126 """Return the list of members of a group."""
128 s = select([self.groups.c.member], and_(self.groups.c.owner==owner,
129 self.groups.c.name==group))
130 r = self.conn.execute(s)
131 l = [row[0] for row in r.fetchall()]
135 def group_check(self, owner, group, member):
136 """Check if a member is in a group."""
138 s = select([self.groups.c.member], and_(self.groups.c.owner==owner,
139 self.groups.c.name==group,
140 self.groups.c.member==member))
141 r = self.conn.execute(s)
146 def group_parents(self, member):
147 """Return all (owner, group) tuples that contain member."""
149 s = select([self.groups.c.owner, self.groups.c.name],
150 self.groups.c.member==member)
151 r = self.conn.execute(s)