Revision 297513ba pithos/api/util.py

b/pithos/api/util.py
45 45
from django.utils.encoding import smart_str
46 46

  
47 47
from pithos.api.compat import parse_http_date_safe, parse_http_date
48
from pithos.api.faults import (Fault, NotModified, BadRequest, Unauthorized, ItemNotFound,
48
from pithos.api.faults import (Fault, NotModified, BadRequest, Unauthorized, Forbidden, ItemNotFound,
49 49
                                Conflict, LengthRequired, PreconditionFailed, RequestEntityTooLarge,
50 50
                                RangeNotSatisfiable, ServiceUnavailable)
51 51
from pithos.backends import connect_backend
......
303 303
                                                        dest_account, dest_container, dest_name,
304 304
                                                        meta, False, permissions, src_version)
305 305
    except NotAllowedError:
306
        raise Unauthorized('Access denied')
306
        raise Forbidden('Not allowed')
307 307
    except (NameError, IndexError):
308 308
        raise ItemNotFound('Container or object does not exist')
309 309
    except ValueError:
......
316 316
        try:
317 317
            request.backend.update_object_public(request.user, dest_account, dest_container, dest_name, public)
318 318
        except NotAllowedError:
319
            raise Unauthorized('Access denied')
319
            raise Forbidden('Not allowed')
320 320
        except NameError:
321 321
            raise ItemNotFound('Object does not exist')
322 322
    return version_id
......
767 767
    
768 768
    return 'text'
769 769

  
770
def api_method(http_method=None, format_allowed=False):
770
def api_method(http_method=None, format_allowed=False, user_required=True):
771 771
    """Decorator function for views that implement an API method."""
772 772
    
773 773
    def decorator(func):
......
776 776
            try:
777 777
                if http_method and request.method != http_method:
778 778
                    raise BadRequest('Method not allowed.')
779
                if user_required and getattr(request, 'user', None) is None:
780
                    raise Unauthorized('Access denied')
779 781
                
780 782
                # The args variable may contain up to (account, container, object).
781 783
                if len(args) > 1 and len(args[1]) > 256:
......
797 799
                fault = ServiceUnavailable('Unexpected error')
798 800
                return render_fault(request, fault)
799 801
            finally:
800
                request.backend.wrapper.conn.close()
802
                if getattr(request, 'backend', None) is not None:
803
                    request.backend.wrapper.conn.close()
801 804
        return wrapper
802 805
    return decorator

Also available in: Unified diff