c5c55453cdb79a8104384f9a8cbc3e363e065e32
[plcapi.git] / PLC / Methods / AddSession.py
1 # $Id$
2 import time
3
4 from PLC.Method import Method
5 from PLC.Parameter import Parameter, Mixed
6 from PLC.Auth import Auth
7 from PLC.Sessions import Session, Sessions
8 from PLC.Persons import Person, Persons
9
10 class AddSession(Method):
11     """
12     Creates and returns a new session key for the specified user. 
13     (Used for website 'user sudo')
14     """
15
16     roles = ['admin']
17     accepts = [
18         Auth(),
19         Mixed(Person.fields['person_id'],
20               Person.fields['email'])
21         ]
22     returns = Session.fields['session_id']
23     
24
25     def call(self, auth, person_id_or_email):
26         
27         persons = Persons(self.api, [person_id_or_email], ['person_id', 'email'])
28         
29         if not persons:
30             raise PLCInvalidArgument, "No such person"
31         
32         person = persons[0]
33         session = Session(self.api)
34         session['expires'] = int(time.time()) + (24 * 60 * 60)
35         session.sync(commit = False)
36         session.add_person(person, commit = True)
37
38         return session['session_id']