Statistics
| Branch: | Tag: | Revision:

root / snf-pithos-backend / pithos / backends / lib / sqlite / groups.py @ 345dcf39

History | View | Annotate | Download (4.6 kB)

1 2e662088 Antony Chazapis
# Copyright 2011-2012 GRNET S.A. All rights reserved.
2 a9b3f29d Antony Chazapis
# 
3 a9b3f29d Antony Chazapis
# Redistribution and use in source and binary forms, with or
4 a9b3f29d Antony Chazapis
# without modification, are permitted provided that the following
5 a9b3f29d Antony Chazapis
# conditions are met:
6 a9b3f29d Antony Chazapis
# 
7 a9b3f29d Antony Chazapis
#   1. Redistributions of source code must retain the above
8 a9b3f29d Antony Chazapis
#      copyright notice, this list of conditions and the following
9 a9b3f29d Antony Chazapis
#      disclaimer.
10 a9b3f29d Antony Chazapis
# 
11 a9b3f29d Antony Chazapis
#   2. Redistributions in binary form must reproduce the above
12 a9b3f29d Antony Chazapis
#      copyright notice, this list of conditions and the following
13 a9b3f29d Antony Chazapis
#      disclaimer in the documentation and/or other materials
14 a9b3f29d Antony Chazapis
#      provided with the distribution.
15 a9b3f29d Antony Chazapis
# 
16 a9b3f29d Antony Chazapis
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 a9b3f29d Antony Chazapis
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 a9b3f29d Antony Chazapis
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 a9b3f29d Antony Chazapis
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 a9b3f29d Antony Chazapis
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 a9b3f29d Antony Chazapis
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 a9b3f29d Antony Chazapis
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 a9b3f29d Antony Chazapis
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 a9b3f29d Antony Chazapis
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 a9b3f29d Antony Chazapis
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 a9b3f29d Antony Chazapis
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 a9b3f29d Antony Chazapis
# POSSIBILITY OF SUCH DAMAGE.
28 a9b3f29d Antony Chazapis
# 
29 a9b3f29d Antony Chazapis
# The views and conclusions contained in the software and
30 a9b3f29d Antony Chazapis
# documentation are those of the authors and should not be
31 a9b3f29d Antony Chazapis
# interpreted as representing official policies, either expressed
32 a9b3f29d Antony Chazapis
# or implied, of GRNET S.A.
33 a9b3f29d Antony Chazapis
34 6f4bce7b Antony Chazapis
from collections import defaultdict
35 6f4bce7b Antony Chazapis
36 a9b3f29d Antony Chazapis
from dbworker import DBWorker
37 a9b3f29d Antony Chazapis
38 a9b3f29d Antony Chazapis
39 a9b3f29d Antony Chazapis
class Groups(DBWorker):
40 a9b3f29d Antony Chazapis
    """Groups are named collections of members, belonging to an owner."""
41 a9b3f29d Antony Chazapis
    
42 a9b3f29d Antony Chazapis
    def __init__(self, **params):
43 a9b3f29d Antony Chazapis
        DBWorker.__init__(self, **params)
44 a9b3f29d Antony Chazapis
        execute = self.execute
45 a9b3f29d Antony Chazapis
        
46 a9b3f29d Antony Chazapis
        execute(""" create table if not exists groups
47 a9b3f29d Antony Chazapis
                          ( owner  text,
48 a9b3f29d Antony Chazapis
                            name   text,
49 a9b3f29d Antony Chazapis
                            member text,
50 a9b3f29d Antony Chazapis
                            primary key (owner, name, member) ) """)
51 a9b3f29d Antony Chazapis
        execute(""" create index if not exists idx_groups_member
52 a9b3f29d Antony Chazapis
                    on groups(member) """)
53 a9b3f29d Antony Chazapis
    
54 a9b3f29d Antony Chazapis
    def group_names(self, owner):
55 a9b3f29d Antony Chazapis
        """List all group names belonging to owner."""
56 a9b3f29d Antony Chazapis
        
57 a9b3f29d Antony Chazapis
        q = "select distinct name from groups where owner = ?"
58 a9b3f29d Antony Chazapis
        self.execute(q, (owner,))
59 a9b3f29d Antony Chazapis
        return [r[0] for r in self.fetchall()]
60 a9b3f29d Antony Chazapis
    
61 6f4bce7b Antony Chazapis
    def group_dict(self, owner):
62 6f4bce7b Antony Chazapis
        """Return a dict mapping group names to member lists for owner."""
63 a9b3f29d Antony Chazapis
        
64 a9b3f29d Antony Chazapis
        q = "select name, member from groups where owner = ?"
65 a9b3f29d Antony Chazapis
        self.execute(q, (owner,))
66 6f4bce7b Antony Chazapis
        d = defaultdict(list)
67 6f4bce7b Antony Chazapis
        for group, member in self.fetchall():
68 6f4bce7b Antony Chazapis
            d[group].append(member)
69 6f4bce7b Antony Chazapis
        return d
70 a9b3f29d Antony Chazapis
    
71 a9b3f29d Antony Chazapis
    def group_add(self, owner, group, member):
72 a9b3f29d Antony Chazapis
        """Add a member to a group."""
73 a9b3f29d Antony Chazapis
        
74 a9b3f29d Antony Chazapis
        q = "insert or ignore into groups (owner, name, member) values (?, ?, ?)"
75 a9b3f29d Antony Chazapis
        self.execute(q, (owner, group, member))
76 a9b3f29d Antony Chazapis
    
77 a9b3f29d Antony Chazapis
    def group_addmany(self, owner, group, members):
78 a9b3f29d Antony Chazapis
        """Add members to a group."""
79 a9b3f29d Antony Chazapis
        
80 a9b3f29d Antony Chazapis
        q = "insert or ignore into groups (owner, name, member) values (?, ?, ?)"
81 a9b3f29d Antony Chazapis
        self.executemany(q, ((owner, group, member) for member in members))
82 a9b3f29d Antony Chazapis
    
83 a9b3f29d Antony Chazapis
    def group_remove(self, owner, group, member):
84 a9b3f29d Antony Chazapis
        """Remove a member from a group."""
85 a9b3f29d Antony Chazapis
        
86 a9b3f29d Antony Chazapis
        q = "delete from groups where owner = ? and name = ? and member = ?"
87 a9b3f29d Antony Chazapis
        self.execute(q, (owner, group, member))
88 a9b3f29d Antony Chazapis
    
89 a9b3f29d Antony Chazapis
    def group_delete(self, owner, group):
90 a9b3f29d Antony Chazapis
        """Delete a group."""
91 a9b3f29d Antony Chazapis
        
92 a9b3f29d Antony Chazapis
        q = "delete from groups where owner = ? and name = ?"
93 a9b3f29d Antony Chazapis
        self.execute(q, (owner, group))
94 a9b3f29d Antony Chazapis
    
95 a9b3f29d Antony Chazapis
    def group_destroy(self, owner):
96 a9b3f29d Antony Chazapis
        """Delete all groups belonging to owner."""
97 a9b3f29d Antony Chazapis
        
98 a9b3f29d Antony Chazapis
        q = "delete from groups where owner = ?"
99 a9b3f29d Antony Chazapis
        self.execute(q, (owner,))
100 a9b3f29d Antony Chazapis
    
101 a9b3f29d Antony Chazapis
    def group_members(self, owner, group):
102 a9b3f29d Antony Chazapis
        """Return the list of members of a group."""
103 a9b3f29d Antony Chazapis
        
104 a9b3f29d Antony Chazapis
        q = "select member from groups where owner = ? and name = ?"
105 a9b3f29d Antony Chazapis
        self.execute(q, (owner, group))
106 a9b3f29d Antony Chazapis
        return [r[0] for r in self.fetchall()]
107 a9b3f29d Antony Chazapis
    
108 a9b3f29d Antony Chazapis
    def group_check(self, owner, group, member):
109 a9b3f29d Antony Chazapis
        """Check if a member is in a group."""
110 a9b3f29d Antony Chazapis
        
111 a9b3f29d Antony Chazapis
        q = "select 1 from groups where owner = ? and name = ? and member = ?"
112 a9b3f29d Antony Chazapis
        self.execute(q, (group, member))
113 a9b3f29d Antony Chazapis
        return bool(self.fetchone())
114 a9b3f29d Antony Chazapis
    
115 a9b3f29d Antony Chazapis
    def group_parents(self, member):
116 a9b3f29d Antony Chazapis
        """Return all (owner, group) tuples that contain member."""
117 a9b3f29d Antony Chazapis
        
118 a9b3f29d Antony Chazapis
        q = "select owner, name from groups where member = ?"
119 a9b3f29d Antony Chazapis
        self.execute(q, (member,))
120 a9b3f29d Antony Chazapis
        return self.fetchall()