source: indico/indico/MaKaC/webinterface/rh/api.py @ c004bc

hello-world-walkthroughipv6v0.98-seriesv0.98.2v0.98.3v0.99v1.0v1.1
Last change on this file since c004bc was c004bc, checked in by Jose Benito <jose.benito.gonzalez@…>, 18 months ago

[IMP] Support persistent api requests (w/o tstamp)

  • Property mode set to 100644
File size: 4.4 KB
Line 
1# -*- coding: utf-8 -*-
2##
3##
4## This file is part of CDS Indico.
5## Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007 CERN.
6##
7## CDS Indico is free software; you can redistribute it and/or
8## modify it under the terms of the GNU General Public License as
9## published by the Free Software Foundation; either version 2 of the
10## License, or (at your option) any later version.
11##
12## CDS Indico is distributed in the hope that it will be useful, but
13## WITHOUT ANY WARRANTY; without even the implied warranty of
14## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15## General Public License for more details.
16##
17## You should have received a copy of the GNU General Public License
18## along with CDS Indico; if not, write to the Free Software Foundation, Inc.,
19## 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
20
21from indico.web.http_api.auth import APIKey, APIKeyHolder
22from indico.web.http_api import API_MODES
23from MaKaC.webinterface.rh.users import RHUserBase
24from MaKaC.webinterface.rh.services import RHServicesBase
25from MaKaC.webinterface import urlHandlers
26from MaKaC.webinterface.pages.api import WPUserAPI, WPAdminAPIOptions, WPAdminAPIKeys
27from MaKaC.errors import AccessError, FormValuesError
28
29class RHUserAPI(RHUserBase):
30    def _process(self):
31        p = WPUserAPI(self, self._avatar)
32        return p.display()
33
34class RHUserAPICreate(RHUserBase):
35    def _checkProtection(self):
36        RHUserBase._checkProtection(self)
37        ak = self._avatar.getAPIKey()
38        if ak and ak.isBlocked():
39            raise AccessError()
40
41    def _process(self):
42        ak = self._avatar.getAPIKey()
43        if not ak:
44            ak = APIKey(self._avatar)
45            ak.create()
46        else:
47            ak.newKey()
48            ak.newSignKey()
49        self._redirect(urlHandlers.UHUserAPI.getURL(self._avatar))
50
51class RHUserAPIPersistent(RHUserBase):
52    def _checkParams(self, params):
53        RHUserBase._checkParams(self, params)
54        self._ak = self._avatar.getAPIKey()
55
56    def _checkProtection(self):
57        RHUserBase._checkProtection(self)
58        ak = self._avatar.getAPIKey()
59        if ak and ak.isBlocked():
60            raise AccessError()
61
62    def _process(self):
63        self._ak.setPersistentAllowed(not self._ak.isPersistentAllowed())
64        self._redirect(urlHandlers.UHUserAPI.getURL(self._avatar))
65
66class RHUserAPIBlock(RHUserBase):
67    def _checkParams(self, params):
68        RHUserBase._checkParams(self, params)
69        self._ak = self._avatar.getAPIKey()
70
71    def _checkProtection(self):
72        RHUserBase._checkProtection(self)
73        if not self._getUser().isAdmin():
74            raise AccessError()
75
76    def _process(self):
77        self._ak.setBlocked(not self._ak.isBlocked())
78        self._redirect(urlHandlers.UHUserAPI.getURL(self._avatar))
79
80class RHUserAPIDelete(RHUserBase):
81    def _checkParams(self, params):
82        RHUserBase._checkParams(self, params)
83        self._ak = self._avatar.getAPIKey()
84
85    def _checkProtection(self):
86        RHUserBase._checkProtection(self)
87        if not self._getUser().isAdmin():
88            raise AccessError()
89
90    def _process(self):
91        self._ak.remove()
92        self._redirect(urlHandlers.UHUserAPI.getURL(self._avatar))
93
94
95class RHAdminAPIOptions(RHServicesBase):
96    def _process(self):
97        p = WPAdminAPIOptions(self)
98        return p.display()
99
100class RHAdminAPIOptionsSet(RHServicesBase):
101    def _checkParams(self, params):
102        RHServicesBase._checkParams(self, params)
103        self._httpsRequired = bool(params.get('httpsRequired'))
104        self._apiMode = int(params.get('apiMode'))
105        try:
106            self._apiCacheTTL = int(params.get('apiCacheTTL', 0))
107            self._apiSignatureTTL = int(params.get('apiSignatureTTL', 0))
108            if self._apiCacheTTL < 0 or self._apiSignatureTTL < 0:
109                raise ValueError
110        except ValueError:
111            raise FormValuesError('TTLs must be positive numbers')
112        if self._apiMode not in API_MODES:
113            raise FormValuesError()
114
115    def _process(self):
116        self._minfo.setAPIHTTPSRequired(self._httpsRequired)
117        self._minfo.setAPIMode(self._apiMode)
118        self._minfo.setAPICacheTTL(self._apiCacheTTL)
119        self._minfo.setAPISignatureTTL(self._apiSignatureTTL)
120        self._redirect(urlHandlers.UHAdminAPIOptions.getURL())
121
122class RHAdminAPIKeys(RHServicesBase):
123    def _process(self):
124        p = WPAdminAPIKeys(self)
125        return p.display()
Note: See TracBrowser for help on using the repository browser.