Statistics
| Branch: | Tag: | Revision:

root / pithos / backends / lib / sqlalchemy / groups.py @ 371d907a

History | View | Annotate | Download (5.7 kB)

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