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