svn keywords
[plcapi.git] / PLC / Methods / GetSession.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.Nodes import Node, Nodes
10 from PLC.Persons import Person, Persons
11
12 class GetSession(Method):
13     """
14     Returns a new session key if a user or node authenticated
15     successfully, faults otherwise.
16     """
17
18     roles = ['admin', 'pi', 'user', 'tech', 'node']
19     accepts = [Auth()]
20     returns = Session.fields['session_id']
21     
22
23     def call(self, auth):
24         # Authenticated with a session key, just return it
25         if auth.has_key('session'):
26             return auth['session']
27
28         session = Session(self.api)
29
30         if isinstance(self.caller, Person):
31             # XXX Make this configurable
32             session['expires'] = int(time.time()) + (24 * 60 * 60)
33
34         session.sync(commit = False)
35
36         if isinstance(self.caller, Node):
37             session.add_node(self.caller, commit = True)
38         elif isinstance(self.caller, Person):
39             session.add_person(self.caller, commit = True)
40
41         return session['session_id']