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