|
1 # Copyright (c) 2012 netzguerilla.net <iro@netzguerilla.net> |
|
2 # |
|
3 # This file is part of Iro. |
|
4 # |
|
5 # Permission is hereby granted, free of charge, to any person obtaining a copy of |
|
6 # this software and associated documentation files (the "Software"), to deal in |
|
7 # the Software without restriction, including without limitation the rights to use, |
|
8 # copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the |
|
9 # #Software, and to permit persons to whom the Software is furnished to do so, |
|
10 # subject to the following conditions: |
|
11 # |
|
12 # The above copyright notice and this permission notice shall be included in |
|
13 # all copies or substantial portions of the Software. |
|
14 # |
|
15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, |
|
16 # INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A |
|
17 # PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
|
18 # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
|
19 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
|
20 # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
|
21 |
|
22 from twisted.internet import defer |
|
23 try: |
|
24 from inspect import getcallargs |
|
25 except ImportError: |
|
26 from ..inspect_getcallargs import getcallargs |
|
27 |
|
28 from decorator import decorator |
|
29 |
|
30 from .schema import User |
|
31 from .dbdefer import dbdefer |
|
32 |
|
33 from ..validate import validate, vHash |
|
34 from ..error import UserNotFound, InterfaceException |
|
35 |
|
36 |
|
37 @validate(kwd="apikey", func=vHash, minlength=15, maxlength=15) |
|
38 @dbdefer |
|
39 def getuser(session, apikey): |
|
40 """Returns a valid user object. |
|
41 |
|
42 :param session: a valid session object (is created by decorator :func:`iro.model.dbdefer.dbdefer`) |
|
43 :param string apikey: a vaild apikey (only [0-9a-f] is allowed) |
|
44 :return: a :class:`iro.model.schema.User` object |
|
45 :raises: :exc:`iro.error.UserNotFound` |
|
46 """ |
|
47 user = session.query(User).filter_by(apikey=apikey).first() |
|
48 if user is None: |
|
49 raise UserNotFound() |
|
50 else: |
|
51 return user |
|
52 |
|
53 @decorator |
|
54 def vUser(f,*args,**kargs): |
|
55 """A Decorator to verify the apikey and execute the function with a valid :class:`iro.model.schema.User` instead of the apikey. |
|
56 |
|
57 The decorator expect the apikey in the **user** parameter. |
|
58 |
|
59 :return: a defer |
|
60 """ |
|
61 kp=getcallargs(f,*args,**kargs) |
|
62 try: |
|
63 apikey = kp["user"] |
|
64 except KeyError: |
|
65 raise InterfaceException() |
|
66 |
|
67 def _gotResult(_user): |
|
68 kp["user"]=_user |
|
69 e = defer.maybeDeferred(f,**kp) |
|
70 return e |
|
71 d = defer.maybeDeferred(getuser, apikey=apikey) |
|
72 return d.addCallback(_gotResult) |
|
73 |
|
74 |