# # SonarQube, open source software quality management tool. # Copyright (C) 2008-2014 SonarSource # mailto:contact AT sonarsource DOT com # # SonarQube is free software; you can redistribute it and/or # modify it under the terms of the GNU Lesser General Public # License as published by the Free Software Foundation; either # version 3 of the License, or (at your option) any later version. # # SonarQube is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public License # along with this program; if not, write to the Free Software Foundation, # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. # # # This class loads your own authorization service from the configuration # in the sonar.properties file. # The property is sonar.authorizer=[Your service file name]. # Before that you should put your service file into the lib/authorization directory. # The service class must be named SonarAuthorizer. # class AuthorizerFactory @@authorizer = nil def self.authorizer if (@@authorizer.nil?) filename = Java::OrgSonarServerUi::JRubyFacade.new.getConfigurationValue('sonar.authorizer') || 'default_authorizer' require File.dirname(__FILE__) + "/authorization/#{filename}" @@authorizer ||= SonarAuthorizer.new end @@authorizer end end # NeedAuthorization is a set of modules that enhance your models and controller classes in authorization function. # All the methods in this module will finally delegate to the loaded SonarAuthorizer. module NeedAuthorization # ForUser module is used for the User class, to decide if the user has certain "global" permissions. module ForUser # # if the parameter 'objects' is nil, then global roles are checked. # The parameter 'objects' can be the project id, a Project, a Snapshot or an array. # # Examples : # # has_role?(:admin) checks the global role 'admin'. It returns a boolean. # has_role?(:admin, 30) checks if the user is administrator of the project 30. It returns a boolean. # has_role?(:admin, [30,45,7]) checks if the user is administrator of the projects 30, 40 and 7. It returns an array of 3 booleans. # def has_role?(role, objects=nil) role = role.to_s if objects.nil? if Internal.permissions.globalPermissions().include?(role) AuthorizerFactory.authorizer.has_role?(self, role.to_sym) else # There's no concept of global users or global codeviewers. # Someone is considered as user if # - authentication is not forced # - authentication is forced and user is authenticated force_authentication = Api::Utils.java_facade.getConfigurationValue('sonar.forceAuthentication')=='true' !force_authentication || self.id end elsif objects.is_a?(Array) has_role_for_resources?(role, objects) else has_role_for_resource?(role, objects) end end def has_role_for_resource?(role, object) has_role_for_resources?(role, [object])[0] end def has_role_for_resources?(role, objects) return [] if objects.nil? || objects.size==0 resource_ids=[] objects.each do |obj| resource_ids< [ :edit, :update ] # # To skip this in a subclassed controller: # # skip_before_filter :admin_required # def admin_required has_role?(:admin) || access_denied end # Inclusion hook to make the methods in this Helper available as ActionView helper methods. def self.included(base) base.send :helper_method, :has_role?, :is_admin?, :is_user?, :admin_required, :select_authorized if base.respond_to? :helper_method end end end