From: Tony Mack <tmack@cs.princeton.edu>
Date: Tue, 10 Oct 2006 20:06:36 +0000 (+0000)
Subject: Initial checkin of new API implementation
X-Git-Tag: pycurl-7_13_1~636
X-Git-Url: http://git.onelab.eu/?a=commitdiff_plain;h=a92fa31d5568781870301a4f82829fc29b260484;p=plcapi.git

Initial checkin of new API implementation
---

diff --git a/PLC/Methods/SetPersonPrimaryKey.py b/PLC/Methods/SetPersonPrimaryKey.py
new file mode 100644
index 00000000..e4c459f5
--- /dev/null
+++ b/PLC/Methods/SetPersonPrimaryKey.py
@@ -0,0 +1,51 @@
+from PLC.Faults import *
+from PLC.Method import Method
+from PLC.Parameter import Parameter, Mixed
+from PLC.Persons import Person, Persons
+from PLC.Keys import Key, Keys
+from PLC.Auth import PasswordAuth
+
+class SetPersonPrimaryKey(Method):
+    """
+    Makes the specified key the person's primary key. The person
+    must already be associated with the key.
+
+    Admins may update anyone. All others may only update themselves.
+    """
+
+    roles = ['admin', 'pi', 'user', 'tech']
+
+    accepts = [
+        PasswordAuth(),
+        Mixed(Person.fields['person_id'],
+              Person.fields['email']),
+        Key.fields['key_id']
+        ]
+
+    returns = Parameter(int, '1 if successful')
+
+    def call(self, auth, person_id_or_email, key_id):
+        # Get account information
+        persons = Persons(self.api, [person_id_or_email])
+        if not persons:
+            raise PLCInvalidArgument, "No such account"
+
+        person = persons.values()[0]
+
+        # Authenticated function
+        assert self.caller is not None
+
+        # Get key information
+        keys = Keys(self.api, [key_id])
+        if not keys:
+            raise PLCInvalidArgument, "No such key"
+
+        key = keys.values()[0]
+
+	if 'admin' not in self.caller['roles']:
+        	if key['key_id'] not in person['key_ids']:
+            		raise PLCInvalidArgument, "Not associated with specified key"
+
+        key.set_primary_key(person)
+
+        return 1