##// END OF EJS Templates
Added isanonymous decorator for checking permissions for anonymous access
marcink -
r779:389d02a5 beta
parent child Browse files
Show More
@@ -1,8 +1,14 b''
1 #!/usr/bin/env python
1 # -*- coding: utf-8 -*-
2 # encoding: utf-8
2 """
3 # settings controller for pylons
3 package.rhodecode.controllers.admin.settings
4 # Copyright (C) 2009-2010 Marcin Kuzminski <marcin@python-works.com>
4 ~~~~~~~~~~~~~~
5 #
5 settings controller for rhodecode admin
6
7 :created_on: Jul 14, 2010
8 :author: marcink
9 :copyright: (C) 2009-2010 Marcin Kuzminski <marcin@python-works.com>
10 :license: GPLv3, see COPYING for more details.
11 """
6 # This program is free software; you can redistribute it and/or
12 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
13 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; version 2
14 # as published by the Free Software Foundation; version 2
@@ -17,11 +23,7 b''
17 # along with this program; if not, write to the Free Software
23 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
24 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19 # MA 02110-1301, USA.
25 # MA 02110-1301, USA.
20 """
26
21 Created on July 14, 2010
22 settings controller for pylons
23 @author: marcink
24 """
25 from formencode import htmlfill
27 from formencode import htmlfill
26 from pylons import request, session, tmpl_context as c, url, app_globals as g, \
28 from pylons import request, session, tmpl_context as c, url, app_globals as g, \
27 config
29 config
@@ -29,7 +31,7 b' from pylons.controllers.util import abor'
29 from pylons.i18n.translation import _
31 from pylons.i18n.translation import _
30 from rhodecode.lib import helpers as h
32 from rhodecode.lib import helpers as h
31 from rhodecode.lib.auth import LoginRequired, HasPermissionAllDecorator, \
33 from rhodecode.lib.auth import LoginRequired, HasPermissionAllDecorator, \
32 HasPermissionAnyDecorator
34 HasPermissionAnyDecorator, NotAnonymous
33 from rhodecode.lib.base import BaseController, render
35 from rhodecode.lib.base import BaseController, render
34 from rhodecode.lib.celerylib import tasks, run_task
36 from rhodecode.lib.celerylib import tasks, run_task
35 from rhodecode.lib.utils import repo2db_mapper, invalidate_cache, \
37 from rhodecode.lib.utils import repo2db_mapper, invalidate_cache, \
@@ -236,13 +238,13 b' class SettingsController(BaseController)'
236 """GET /admin/settings/setting_id/edit: Form to edit an existing item"""
238 """GET /admin/settings/setting_id/edit: Form to edit an existing item"""
237 # url('admin_edit_setting', setting_id=ID)
239 # url('admin_edit_setting', setting_id=ID)
238
240
239
241 @NotAnonymous()
240 def my_account(self):
242 def my_account(self):
241 """
243 """
242 GET /_admin/my_account Displays info about my account
244 GET /_admin/my_account Displays info about my account
243 """
245 """
246 # url('admin_settings_my_account')
244
247
245 # url('admin_settings_my_account')
246 c.user = UserModel().get(c.rhodecode_user.user_id, cache=False)
248 c.user = UserModel().get(c.rhodecode_user.user_id, cache=False)
247 all_repos = self.sa.query(Repository)\
249 all_repos = self.sa.query(Repository)\
248 .filter(Repository.user_id == c.user.user_id)\
250 .filter(Repository.user_id == c.user.user_id)\
@@ -280,7 +282,7 b' class SettingsController(BaseController)'
280 try:
282 try:
281 form_result = _form.to_python(dict(request.POST))
283 form_result = _form.to_python(dict(request.POST))
282 user_model.update_my_account(uid, form_result)
284 user_model.update_my_account(uid, form_result)
283 h.flash(_('Your account was updated succesfully'),
285 h.flash(_('Your account was updated successfully'),
284 category='success')
286 category='success')
285
287
286 except formencode.Invalid, errors:
288 except formencode.Invalid, errors:
@@ -300,11 +302,12 b' class SettingsController(BaseController)'
300 encoding="UTF-8")
302 encoding="UTF-8")
301 except Exception:
303 except Exception:
302 log.error(traceback.format_exc())
304 log.error(traceback.format_exc())
303 h.flash(_('error occured during update of user %s') \
305 h.flash(_('error occurred during update of user %s') \
304 % form_result.get('username'), category='error')
306 % form_result.get('username'), category='error')
305
307
306 return redirect(url('my_account'))
308 return redirect(url('my_account'))
307
309
310 @NotAnonymous()
308 @HasPermissionAnyDecorator('hg.admin', 'hg.create.repository')
311 @HasPermissionAnyDecorator('hg.admin', 'hg.create.repository')
309 def create_repository(self):
312 def create_repository(self):
310 """GET /_admin/create_repository: Form to create a new item"""
313 """GET /_admin/create_repository: Form to create a new item"""
@@ -297,7 +297,8 b' def get_user(session):'
297 # CHECK DECORATORS
297 # CHECK DECORATORS
298 #===============================================================================
298 #===============================================================================
299 class LoginRequired(object):
299 class LoginRequired(object):
300 """Must be logged in to execute this function else redirect to login page"""
300 """Must be logged in to execute this function else
301 redirect to login page"""
301
302
302 def __call__(self, func):
303 def __call__(self, func):
303 return decorator(self.__wrapper, func)
304 return decorator(self.__wrapper, func)
@@ -322,6 +323,31 b' class LoginRequired(object):'
322 log.debug('redirecting to login page with %s', p)
323 log.debug('redirecting to login page with %s', p)
323 return redirect(url('login_home', came_from=p))
324 return redirect(url('login_home', came_from=p))
324
325
326 class NotAnonymous(object):
327 """Must be logged in to execute this function else
328 redirect to login page"""
329
330 def __call__(self, func):
331 return decorator(self.__wrapper, func)
332
333 def __wrapper(self, func, *fargs, **fkwargs):
334 user = session.get('rhodecode_user', AuthUser())
335 log.debug('Checking if user is not anonymous')
336
337 anonymous = user.username == 'default'
338
339 if anonymous:
340 p = ''
341 if request.environ.get('SCRIPT_NAME') != '/':
342 p += request.environ.get('SCRIPT_NAME')
343
344 p += request.environ.get('PATH_INFO')
345 if request.environ.get('QUERY_STRING'):
346 p += '?' + request.environ.get('QUERY_STRING')
347 return redirect(url('login_home', came_from=p))
348 else:
349 return func(*fargs, **fkwargs)
350
325 class PermsDecorator(object):
351 class PermsDecorator(object):
326 """Base class for decorators"""
352 """Base class for decorators"""
327
353
General Comments 0
You need to be logged in to leave comments. Login now