rosa-build/app/controllers/api/v1/platforms_controller.rb

96 lines
2.7 KiB
Ruby
Raw Normal View History

# -*- encoding : utf-8 -*-
class Api::V1::PlatformsController < Api::V1::BaseController
before_filter :authenticate_user!
skip_before_filter :authenticate_user!, :only => :allowed
skip_before_filter :authenticate_user!, :only => [:show, :platforms_for_build, :members] if APP_CONFIG['anonymous_access']
2013-06-26 13:07:55 +01:00
load_and_authorize_resource :except => :allowed
def allowed
platform_name = (params[:path] || '').gsub(/^[\/]+/, '')
.match(/^(#{Platform::NAME_PATTERN}\/|#{Platform::NAME_PATTERN}$)/)
render(:nothing => true) && return unless platform_name
2013-07-02 18:21:59 +01:00
platform_name = platform_name[0].gsub(/\//, '')
2013-07-02 18:21:59 +01:00
platform = Platform.find_by_name platform_name
render(:nothing => true, :status => 403) && return unless platform
render(:nothing => true) && return unless platform.hidden?
if request.authorization.present?
token, pass = *ActionController::HttpAuthentication::Basic::user_name_and_password(request)
else
render(:nothing => true, :status => 403) && return
end
render(:nothing => true) && return if platform.tokens.by_active.where(:authentication_token => token).exists?
user = User.find_by_authentication_token token
@current_ability, @current_user = nil, user
if user && can?(:show, platform)
render :nothing => true
else
render :nothing => true, :status => 403
end
end
def index
2012-10-04 14:34:30 +01:00
@platforms = @platforms.accessible_by(current_ability, :related).
by_type(params[:type]).paginate(paginate_params)
end
def show
end
def platforms_for_build
@platforms = Platform.main.opened.paginate(paginate_params)
render :index
end
def create
platform_params = params[:platform] || {}
owner = User.where(:id => platform_params[:owner_id]).first
@platform.owner = owner || get_owner
2012-10-16 13:35:30 +01:00
create_subject @platform
end
def update
2012-10-10 17:31:22 +01:00
platform_params = params[:platform] || {}
owner = User.where(:id => platform_params[:owner_id]).first
platform_params[:owner] = owner if owner
2012-10-14 13:39:58 +01:00
update_subject @platform
end
def members
@members = @platform.members.order('name').paginate(paginate_params)
end
def add_member
2012-10-14 13:39:58 +01:00
add_member_to_subject @platform
end
def remove_member
2012-10-14 13:39:58 +01:00
remove_member_from_subject @platform
end
def clone
2012-10-10 17:31:22 +01:00
platform_params = params[:platform] || {}
platform_params[:owner] = current_user
@cloned = @platform.full_clone(platform_params)
if @cloned.persisted?
2012-10-11 16:36:14 +01:00
render_json_response @platform, 'Platform has been cloned successfully'
else
2012-10-11 16:36:14 +01:00
render_validation_error @platform, 'Platform has not been cloned'
end
end
def clear
@platform.clear
2012-10-11 16:36:14 +01:00
render_json_response @platform, 'Platform has been cleared successfully'
end
def destroy
2012-10-14 13:39:58 +01:00
destroy_subject @platform
end
end