Merge remote-tracking branch 'origin/pycurl' into planetlab-4_0-branch
[plcapi.git] / PLC / Methods / GetSession.py
1 import time
2
3 from PLC.Method import Method
4 from PLC.Parameter import Parameter, Mixed
5 from PLC.Auth import Auth
6 from PLC.Sessions import Session, Sessions
7 from PLC.Nodes import Node, Nodes
8 from PLC.Persons import Person, Persons
9
10 class GetSession(Method):
11     """
12     Returns a new session key if a user or node authenticated
13     successfully, faults otherwise.
14     """
15
16     roles = ['admin', 'pi', 'user', 'tech', 'node']
17     accepts = [Auth()]
18     returns = Session.fields['session_id']
19     
20
21     def call(self, auth):
22         # Authenticated with a session key, just return it
23         if auth.has_key('session'):
24             return auth['session']
25
26         session = Session(self.api)
27
28         if isinstance(self.caller, Person):
29             # XXX Make this configurable
30             session['expires'] = int(time.time()) + (24 * 60 * 60)
31
32         session.sync(commit = False)
33
34         if isinstance(self.caller, Node):
35             session.add_node(self.caller, commit = True)
36         elif isinstance(self.caller, Person):
37             session.add_person(self.caller, commit = True)
38
39         return session['session_id']