2013-04-02 00:04:27 +04:00
# Copyright (c) 2013 AnsibleWorks, Inc.
2013-03-24 02:43:11 +04:00
#
2013-04-09 09:05:55 +04:00
# This file is part of Ansible Commander.
#
2013-03-24 02:43:11 +04:00
# Ansible Commander is free software: you can redistribute it and/or modify
2013-04-09 09:05:55 +04:00
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 3 of the License.
2013-03-24 02:43:11 +04:00
#
2013-04-09 09:05:55 +04:00
# Ansible Commander is distributed in the hope that it will be useful,
2013-03-24 02:43:11 +04:00
# but WITHOUT ANY WARRANTY; without even the implied warranty of
2013-04-09 09:05:55 +04:00
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible Commander. If not, see <http://www.gnu.org/licenses/>.
2013-03-24 02:43:11 +04:00
2013-03-21 08:34:59 +04:00
from lib . main . models import *
from lib . main . serializers import *
from rest_framework import permissions
from django . contrib . auth . models import AnonymousUser
2013-03-21 18:25:49 +04:00
from django . core . exceptions import PermissionDenied
2013-03-21 22:20:59 +04:00
from django . http import Http404
2013-03-21 08:34:59 +04:00
# FIXME: this will probably need to be subclassed by object type
class CustomRbac ( permissions . BasePermission ) :
def _common_user_check ( self , request ) :
# no anonymous users
2013-03-22 19:37:15 +04:00
if request . user . is_anonymous ( ) :
2013-03-21 18:25:49 +04:00
# 401, not 403, hence no raised exception
2013-03-21 08:34:59 +04:00
return False
# superusers are always good
if request . user . is_superuser :
return True
# other users must have associated acom user records & be active
2013-03-22 19:35:26 +04:00
if not request . user . is_active :
2013-03-21 18:25:49 +04:00
raise PermissionDenied ( )
2013-03-21 08:34:59 +04:00
return True
def has_permission ( self , request , view , obj = None ) :
if not self . _common_user_check ( request ) :
return False
if obj is None :
2013-03-21 19:06:47 +04:00
if getattr ( view , ' list_permissions_check ' , None ) :
if request . user . is_superuser :
return True
if not view . list_permissions_check ( request ) :
raise PermissionDenied ( )
elif not getattr ( view , ' item_permissions_check ' , None ) :
raise Exception ( " internal error, list_permissions_check or item_permissions_check must be defined " )
2013-03-21 08:34:59 +04:00
return True
else :
# haven't tested around these confines yet
2013-03-21 18:25:49 +04:00
raise Exception ( " did not expect to get to this position " )
2013-03-21 08:34:59 +04:00
def has_object_permission ( self , request , view , obj ) :
2013-03-24 22:14:59 +04:00
if isinstance ( obj , User ) :
2013-03-24 20:36:42 +04:00
if not obj . is_active :
raise Http404 ( )
else :
if not obj . active :
raise Http404 ( )
2013-03-24 22:14:59 +04:00
if request . user . is_superuser :
return True
if not self . _common_user_check ( request ) :
return False
2013-03-21 19:06:47 +04:00
if not view . item_permissions_check ( request , obj ) :
2013-03-21 18:25:49 +04:00
raise PermissionDenied ( )
2013-03-21 08:34:59 +04:00
return True
2013-03-23 23:34:16 +04:00