Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (4.4 kB)

1 2e662088 Antony Chazapis
# Copyright 2011-2012 GRNET S.A. All rights reserved.
2 2715ade4 Sofia Papagiannaki
#
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 2715ade4 Sofia Papagiannaki
#
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 2715ade4 Sofia Papagiannaki
#
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 2715ade4 Sofia Papagiannaki
#
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 2715ade4 Sofia Papagiannaki
#
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 2715ade4 Sofia Papagiannaki
42 a9b3f29d Antony Chazapis
    def __init__(self, **params):
43 a9b3f29d Antony Chazapis
        DBWorker.__init__(self, **params)
44 a9b3f29d Antony Chazapis
        execute = self.execute
45 2715ade4 Sofia Papagiannaki
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 2715ade4 Sofia Papagiannaki
54 a9b3f29d Antony Chazapis
    def group_names(self, owner):
55 a9b3f29d Antony Chazapis
        """List all group names belonging to owner."""
56 2715ade4 Sofia Papagiannaki
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 2715ade4 Sofia Papagiannaki
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 2715ade4 Sofia Papagiannaki
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 2715ade4 Sofia Papagiannaki
71 a9b3f29d Antony Chazapis
    def group_add(self, owner, group, member):
72 a9b3f29d Antony Chazapis
        """Add a member to a group."""
73 2715ade4 Sofia Papagiannaki
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 2715ade4 Sofia Papagiannaki
77 a9b3f29d Antony Chazapis
    def group_addmany(self, owner, group, members):
78 a9b3f29d Antony Chazapis
        """Add members to a group."""
79 2715ade4 Sofia Papagiannaki
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 2715ade4 Sofia Papagiannaki
83 a9b3f29d Antony Chazapis
    def group_remove(self, owner, group, member):
84 a9b3f29d Antony Chazapis
        """Remove a member from a group."""
85 2715ade4 Sofia Papagiannaki
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 2715ade4 Sofia Papagiannaki
89 a9b3f29d Antony Chazapis
    def group_delete(self, owner, group):
90 a9b3f29d Antony Chazapis
        """Delete a group."""
91 2715ade4 Sofia Papagiannaki
92 a9b3f29d Antony Chazapis
        q = "delete from groups where owner = ? and name = ?"
93 a9b3f29d Antony Chazapis
        self.execute(q, (owner, group))
94 2715ade4 Sofia Papagiannaki
95 a9b3f29d Antony Chazapis
    def group_destroy(self, owner):
96 a9b3f29d Antony Chazapis
        """Delete all groups belonging to owner."""
97 2715ade4 Sofia Papagiannaki
98 a9b3f29d Antony Chazapis
        q = "delete from groups where owner = ?"
99 a9b3f29d Antony Chazapis
        self.execute(q, (owner,))
100 2715ade4 Sofia Papagiannaki
101 a9b3f29d Antony Chazapis
    def group_members(self, owner, group):
102 a9b3f29d Antony Chazapis
        """Return the list of members of a group."""
103 2715ade4 Sofia Papagiannaki
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 2715ade4 Sofia Papagiannaki
108 a9b3f29d Antony Chazapis
    def group_check(self, owner, group, member):
109 a9b3f29d Antony Chazapis
        """Check if a member is in a group."""
110 2715ade4 Sofia Papagiannaki
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 2715ade4 Sofia Papagiannaki
115 a9b3f29d Antony Chazapis
    def group_parents(self, member):
116 a9b3f29d Antony Chazapis
        """Return all (owner, group) tuples that contain member."""
117 2715ade4 Sofia Papagiannaki
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()