40c32771e73c134863493d740a36dc9868c7ca28
[plcapi.git] / PLC / Roles.py
1 #
2 # Functions for interacting with the roles table in the database
3 #
4 # Mark Huang <mlhuang@cs.princeton.edu>
5 # Copyright (C) 2006 The Trustees of Princeton University
6 #
7 # $Id: Roles.py,v 1.2 2006/10/06 18:19:41 mlhuang Exp $
8 #
9
10 from types import StringTypes
11 from PLC.Faults import *
12 from PLC.Parameter import Parameter
13 from PLC.Table import Row, Table
14
15 class Role(Row):
16     """
17     Representation of a row in the roles table. To use,
18     instantiate with a dict of values.
19     """
20
21     table_name = 'roles'
22     primary_key = 'role_id'
23     fields = {
24         'role_id': Parameter(int, "Role identifier"),
25         'name': Parameter(str, "Role", max = 100),
26         }
27
28     def __init__(self, api, fields = {}):
29         Row.__init__(self, fields)
30         self.api = api
31
32     def validate_role_id(self, role_id):
33         # Make sure role does not already exist
34         conflicts = Roles(self.api, [role_id])
35         if conflicts:
36             raise PLCInvalidArgument, "Role ID already in use"
37
38         return role_id
39
40     def validate_name(self, name):
41         # Remove leading and trailing spaces
42         name = name.strip()
43
44         # Make sure name is not blank after we removed the spaces
45         if not name:
46             raise PLCInvalidArgument, "Role must be specified"
47         
48         # Make sure role does not already exist
49         conflicts = Roles(self.api, [name])
50         if conflicts:
51             raise PLCInvalidArgument, "Role name already in use"
52
53         return name
54
55     def delete(self, commit = True):
56         assert 'role_id' in self
57
58         # Clean up miscellaneous join tables
59         for table in ['person_role', 'roles']:
60             self.api.db.do("DELETE FROM %s" \
61                            " WHERE role_id = %d" % \
62                            (table, self['role_id']), self)
63
64         self.api.db.do("DELETE FROM attributes WHERE min_role_id = %d" % \
65                        self['role_id'])
66
67         if commit:
68             self.api.db.commit()
69         
70 class Roles(Table):
71     """
72     Representation of the roles table in the database.
73     """
74
75     def __init__(self, api, role_id_or_name_list = None):
76         sql = "SELECT %s FROM roles" % \
77               ", ".join(Role.fields)
78         
79         if role_id_or_name_list:
80             # Separate the list into integers and strings
81             role_ids = filter(lambda role_id: isinstance(role_id, (int, long)),
82                                    role_id_or_name_list)
83             names = filter(lambda name: isinstance(name, StringTypes),
84                            role_id_or_name_list)
85             sql += " WHERE (False"
86             if role_ids:
87                 sql += " OR role_id IN (%s)" % ", ".join(map(str, role_ids))
88             if names:
89                 sql += " OR name IN (%s)" % ", ".join(api.db.quote(names))
90             sql += ")"
91
92         rows = api.db.selectall(sql)
93
94         for row in rows:
95             self[row['role_id']] = Role(api, row)