(*) slice atttribute types get cached
[plcapi.git] / PLC / Methods / AddRoleToPerson.py
index 25b54e2..279fd71 100644 (file)
@@ -2,15 +2,15 @@ from PLC.Faults import *
 from PLC.Method import Method
 from PLC.Parameter import Parameter, Mixed
 from PLC.Persons import Person, Persons
-from PLC.Auth import PasswordAuth
-from PLC.Roles import Roles
+from PLC.Auth import Auth
+from PLC.Roles import Role, Roles
 
 class AddRoleToPerson(Method):
     """
     Grants the specified role to the person.
     
     PIs can only grant the tech and user roles to users and techs at
-    their sites. ins can grant any role to any user.
+    their sites. Admins can grant any role to any user.
 
     Returns 1 if successful, faults otherwise.
     """
@@ -18,18 +18,25 @@ class AddRoleToPerson(Method):
     roles = ['admin', 'pi']
 
     accepts = [
-        PasswordAuth(),
+        Auth(),
+        Mixed(Role.fields['role_id'],
+              Role.fields['name']),
         Mixed(Person.fields['person_id'],
               Person.fields['email']),
-        Mixed(Parameter(int, "Role identifier"),
-              Parameter(str, "Role name"))
         ]
 
     returns = Parameter(int, '1 if successful')
 
-    def call(self, auth, person_id_or_email, role_id_or_name):
+    event_type = 'AddTo'
+    object_type = 'Person'
+
+    def call(self, auth, role_id_or_name, person_id_or_email):
         # Get all roles
-        roles = Roles(self.api)
+        roles = {}
+        for role in Roles(self.api):
+            roles[role['role_id']] = role['name']
+            roles[role['name']] = role['role_id']
+
         if role_id_or_name not in roles:
             raise PLCInvalidArgument, "Invalid role identifier or name"
 
@@ -43,7 +50,7 @@ class AddRoleToPerson(Method):
         if not persons:
             raise PLCInvalidArgument, "No such account"
 
-        person = persons.values()[0]
+        person = persons[0]
 
         # Authenticated function
         assert self.caller is not None
@@ -60,4 +67,6 @@ class AddRoleToPerson(Method):
         if role_id not in person['role_ids']:
             person.add_role(role_id)
 
+       self.object_ids = [person['person_id']]
+
         return 1