lib/cuba_api/guard.rb in cuba-api-0.6.2 vs lib/cuba_api/guard.rb in cuba-api-0.6.3

- old
+ new

@@ -21,10 +21,14 @@ # -*- Coding: utf-8 -*- require 'ixtlan/user_management/guard' # TODO move to upstream +class Ixtlan::UserManagement::GuardException < StandardError +end + +# TODO move to upstream class Ixtlan::UserManagement::Permission attribute :parent, Ixtlan::UserManagement::Permission end module CubaApi @@ -46,23 +50,33 @@ logger_factory.logger( "CubaApi::Guard" ) end end def current_groups - current_user.groups + if current_user + current_user.groups + else + [] + end end def allowed_associations guard.associations( guard_context, @_method ) end def on_context( name, &block ) - guard.check_parent( name, guard_context ) on name do begin + guard.check_parent( name, guard_context ) old = guard_context guard_context( name ) yield( *captures ) + rescue Ixtlan::UserManagement::GuardException + if respond_to?( :authenticated? ) && authenticated? + no_body :not_found + else + no_body :forbidden + end ensure guard_context( old ) end end end