Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (4.5 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 29148653 Sofia Papagiannaki
        q = ("insert or ignore into groups (owner, name, member) "
75 29148653 Sofia Papagiannaki
             "values (?, ?, ?)")
76 a9b3f29d Antony Chazapis
        self.execute(q, (owner, group, member))
77 2715ade4 Sofia Papagiannaki
78 a9b3f29d Antony Chazapis
    def group_addmany(self, owner, group, members):
79 a9b3f29d Antony Chazapis
        """Add members to a group."""
80 2715ade4 Sofia Papagiannaki
81 29148653 Sofia Papagiannaki
        q = ("insert or ignore into groups (owner, name, member) "
82 29148653 Sofia Papagiannaki
             "values (?, ?, ?)")
83 a9b3f29d Antony Chazapis
        self.executemany(q, ((owner, group, member) for member in members))
84 2715ade4 Sofia Papagiannaki
85 a9b3f29d Antony Chazapis
    def group_remove(self, owner, group, member):
86 a9b3f29d Antony Chazapis
        """Remove a member from a group."""
87 2715ade4 Sofia Papagiannaki
88 a9b3f29d Antony Chazapis
        q = "delete from groups where owner = ? and name = ? and member = ?"
89 a9b3f29d Antony Chazapis
        self.execute(q, (owner, group, member))
90 2715ade4 Sofia Papagiannaki
91 a9b3f29d Antony Chazapis
    def group_delete(self, owner, group):
92 a9b3f29d Antony Chazapis
        """Delete a group."""
93 2715ade4 Sofia Papagiannaki
94 a9b3f29d Antony Chazapis
        q = "delete from groups where owner = ? and name = ?"
95 a9b3f29d Antony Chazapis
        self.execute(q, (owner, group))
96 2715ade4 Sofia Papagiannaki
97 a9b3f29d Antony Chazapis
    def group_destroy(self, owner):
98 a9b3f29d Antony Chazapis
        """Delete all groups belonging to owner."""
99 2715ade4 Sofia Papagiannaki
100 a9b3f29d Antony Chazapis
        q = "delete from groups where owner = ?"
101 a9b3f29d Antony Chazapis
        self.execute(q, (owner,))
102 2715ade4 Sofia Papagiannaki
103 a9b3f29d Antony Chazapis
    def group_members(self, owner, group):
104 a9b3f29d Antony Chazapis
        """Return the list of members of a group."""
105 2715ade4 Sofia Papagiannaki
106 a9b3f29d Antony Chazapis
        q = "select member from groups where owner = ? and name = ?"
107 a9b3f29d Antony Chazapis
        self.execute(q, (owner, group))
108 a9b3f29d Antony Chazapis
        return [r[0] for r in self.fetchall()]
109 2715ade4 Sofia Papagiannaki
110 a9b3f29d Antony Chazapis
    def group_check(self, owner, group, member):
111 a9b3f29d Antony Chazapis
        """Check if a member is in a group."""
112 2715ade4 Sofia Papagiannaki
113 a9b3f29d Antony Chazapis
        q = "select 1 from groups where owner = ? and name = ? and member = ?"
114 a9b3f29d Antony Chazapis
        self.execute(q, (group, member))
115 a9b3f29d Antony Chazapis
        return bool(self.fetchone())
116 2715ade4 Sofia Papagiannaki
117 a9b3f29d Antony Chazapis
    def group_parents(self, member):
118 a9b3f29d Antony Chazapis
        """Return all (owner, group) tuples that contain member."""
119 2715ade4 Sofia Papagiannaki
120 a9b3f29d Antony Chazapis
        q = "select owner, name from groups where member = ?"
121 a9b3f29d Antony Chazapis
        self.execute(q, (member,))
122 a9b3f29d Antony Chazapis
        return self.fetchall()