diff options
| author | Atul Patel (OpenERP) <atp@tinyerp.com> | 2014-02-18 11:26:18 +0530 |
|---|---|---|
| committer | Atul Patel (OpenERP) <atp@tinyerp.com> | 2014-02-18 11:26:18 +0530 |
| commit | 33b99c8a0737e08e6a243e41bb455d3ab7e6c851 (patch) | |
| tree | a1db52bfbeecf0c6fd9096604b7ec4540bb04197 /addons/auth_oauth/controllers | |
| parent | caa3510ea434e10c32dcbc134f9c74b2b9f18b2a (diff) | |
| parent | aa0eac1257b97c1be693e8a804fec6ecbf2bb22f (diff) | |
[MERGE]: Merged with addons
bzr revid: atp@tinyerp.com-20140218055618-r36w29vat7zig9ar
Diffstat (limited to 'addons/auth_oauth/controllers')
| -rw-r--r-- | addons/auth_oauth/controllers/main.py | 77 |
1 files changed, 58 insertions, 19 deletions
diff --git a/addons/auth_oauth/controllers/main.py b/addons/auth_oauth/controllers/main.py index af8b36d2556..e660fd8363b 100644 --- a/addons/auth_oauth/controllers/main.py +++ b/addons/auth_oauth/controllers/main.py @@ -8,9 +8,10 @@ from werkzeug.exceptions import BadRequest import openerp from openerp import SUPERUSER_ID from openerp import http -from openerp.http import request +from openerp.http import request, LazyResponse from openerp.addons.web.controllers.main import db_monodb, set_cookie_and_redirect, login_and_redirect from openerp.modules.registry import RegistryManager +from openerp.tools.translate import _ _logger = logging.getLogger(__name__) @@ -34,22 +35,60 @@ def fragment_to_query_string(func): return func(self, *a, **kw) return wrapper - #---------------------------------------------------------- # Controller #---------------------------------------------------------- -class OAuthController(http.Controller): - - @http.route('/auth_oauth/list_providers', type='json', auth='none') - def list_providers(self, dbname): +class OAuthLogin(openerp.addons.web.controllers.main.Home): + def list_providers(self): try: - registry = RegistryManager.get(dbname) - with registry.cursor() as cr: - providers = registry.get('auth.oauth.provider') - l = providers.read(cr, SUPERUSER_ID, providers.search(cr, SUPERUSER_ID, [('enabled', '=', True)])) + provider_obj = request.registry.get('auth.oauth.provider') + providers = provider_obj.search_read(request.cr, SUPERUSER_ID, [('enabled', '=', True)]) except Exception: - l = [] - return l + providers = [] + for provider in providers: + return_url = request.httprequest.url_root + 'auth_oauth/signin' + state = self.get_state(provider) + params = dict( + debug=request.debug, + response_type='token', + client_id=provider['client_id'], + redirect_uri=return_url, + scope=provider['scope'], + state=simplejson.dumps(state), + ) + provider['auth_link'] = provider['auth_endpoint'] + '?' + werkzeug.url_encode(params) + + return providers + + def get_state(self, provider): + return dict( + d=request.session.db, + p=provider['id'] + ) + + @http.route() + def web_login(self, *args, **kw): + providers = self.list_providers() + + response = super(OAuthLogin, self).web_login(*args, **kw) + if isinstance(response, LazyResponse): + error = request.params.get('oauth_error') + if error == '1': + error = _("Sign up is not allowed on this database.") + elif error == '2': + error = _("Access Denied") + elif error == '3': + error = _("You do not have access to this database or your invitation has expired. Please ask for an invitation and be sure to follow the link in your invitation email.") + else: + error = None + + response.params['values']['providers'] = providers + if error: + response.params['values']['error'] = error + + return response + +class OAuthController(http.Controller): @http.route('/auth_oauth/signin', type='http', auth='none') @fragment_to_query_string @@ -66,27 +105,27 @@ class OAuthController(http.Controller): cr.commit() action = state.get('a') menu = state.get('m') - url = '/' + url = '/web' if action: - url = '/#action=%s' % action + url = '/web#action=%s' % action elif menu: - url = '/#menu_id=%s' % menu + url = '/web#menu_id=%s' % menu return login_and_redirect(*credentials, redirect_url=url) except AttributeError: # auth_signup is not installed _logger.error("auth_signup not installed on database %s: oauth sign up cancelled." % (dbname,)) - url = "/#action=login&oauth_error=1" + url = "/web/login?oauth_error=1" except openerp.exceptions.AccessDenied: # oauth credentials not valid, user could be on a temporary session _logger.info('OAuth2: access denied, redirect to main page in case a valid session exists, without setting cookies') - url = "/#action=login&oauth_error=3" + url = "/web/login?oauth_error=3" redirect = werkzeug.utils.redirect(url, 303) redirect.autocorrect_location_header = False return redirect except Exception, e: # signup error _logger.exception("OAuth2: %s" % str(e)) - url = "/#action=login&oauth_error=2" + url = "/web/login?oauth_error=2" return set_cookie_and_redirect(url) @@ -105,7 +144,7 @@ class OAuthController(http.Controller): try: model, provider_id = IMD.get_object_reference(cr, SUPERUSER_ID, 'auth_oauth', 'provider_openerp') except ValueError: - return set_cookie_and_redirect('/?db=%s' % dbname) + return set_cookie_and_redirect('/web?db=%s' % dbname) assert model == 'auth.oauth.provider' state = { |
