2012-08-17 14:40:57 +01:00
|
|
|
# -*- encoding : utf-8 -*-
|
2012-09-27 13:13:59 +01:00
|
|
|
class Api::V1::PlatformsController < Api::V1::BaseController
|
2012-09-26 18:15:11 +01:00
|
|
|
before_filter :authenticate_user!
|
2013-06-26 13:04:06 +01:00
|
|
|
skip_before_filter :authenticate_user!, :only => :allowed
|
2012-10-09 17:23:48 +01:00
|
|
|
skip_before_filter :authenticate_user!, :only => [:show, :platforms_for_build, :members] if APP_CONFIG['anonymous_access']
|
2012-08-17 14:40:57 +01:00
|
|
|
|
2013-06-26 13:07:55 +01:00
|
|
|
load_and_authorize_resource :except => :allowed
|
2013-06-26 13:04:06 +01:00
|
|
|
|
|
|
|
def allowed
|
2013-07-02 18:21:59 +01:00
|
|
|
platform_name = (params[:path] || '').match(/^\/[\w]+\//)
|
|
|
|
render(:inline => 'true') && return unless platform_name
|
|
|
|
platform_name = platform_name[0].gsub(/\//, '')
|
2013-06-26 15:23:36 +01:00
|
|
|
|
2013-07-02 18:21:59 +01:00
|
|
|
platform = Platform.find_by_name platform_name
|
2013-06-26 17:31:24 +01:00
|
|
|
render(:inline => 'false', :status => 403) && return unless platform
|
|
|
|
render(:inline => 'true') && return unless platform.hidden?
|
|
|
|
|
2013-07-02 18:21:59 +01:00
|
|
|
token, pass = *ActionController::HttpAuthentication::Basic::user_name_and_password(request)
|
2013-07-02 18:41:12 +01:00
|
|
|
|
|
|
|
render(:inline => 'true') && return if platform.tokens.where(:authentication_token => token).exists?
|
|
|
|
|
2013-06-26 17:31:24 +01:00
|
|
|
user = User.find_by_authentication_token token
|
|
|
|
@current_ability, @current_user = nil, user
|
|
|
|
if user && can?(:read, platform)
|
2013-06-26 15:23:36 +01:00
|
|
|
render :inline => 'true'
|
2013-06-26 13:04:06 +01:00
|
|
|
else
|
2013-06-26 15:23:36 +01:00
|
|
|
render :inline => 'false', :status => 403
|
2013-06-26 13:04:06 +01:00
|
|
|
end
|
2013-07-02 18:31:04 +01:00
|
|
|
rescue => e
|
|
|
|
render :inline => 'false', :status => 403
|
2013-06-26 13:04:06 +01:00
|
|
|
end
|
2012-08-17 14:40:57 +01:00
|
|
|
|
|
|
|
def index
|
2012-10-04 14:34:30 +01:00
|
|
|
@platforms = @platforms.accessible_by(current_ability, :related).
|
2012-10-09 16:06:27 +01:00
|
|
|
by_type(params[:type]).paginate(paginate_params)
|
2012-08-17 14:40:57 +01:00
|
|
|
end
|
2012-09-26 18:15:11 +01:00
|
|
|
|
|
|
|
def show
|
2012-10-08 19:02:44 +01:00
|
|
|
end
|
2012-09-26 18:15:11 +01:00
|
|
|
|
2012-10-08 19:02:44 +01:00
|
|
|
def platforms_for_build
|
2012-10-09 16:06:27 +01:00
|
|
|
@platforms = Platform.main.opened.paginate(paginate_params)
|
2012-10-08 19:02:44 +01:00
|
|
|
render :index
|
2012-09-26 18:15:11 +01:00
|
|
|
end
|
2012-10-09 15:46:20 +01:00
|
|
|
|
2012-10-10 18:45:56 +01:00
|
|
|
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
|
2012-10-10 18:45:56 +01:00
|
|
|
end
|
|
|
|
|
2012-10-09 15:46:20 +01:00
|
|
|
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
|
2012-10-09 15:46:20 +01:00
|
|
|
end
|
|
|
|
|
2012-10-09 16:06:27 +01:00
|
|
|
def members
|
|
|
|
@members = @platform.members.order('name').paginate(paginate_params)
|
|
|
|
end
|
|
|
|
|
2012-10-09 17:23:48 +01:00
|
|
|
def add_member
|
2012-10-14 13:39:58 +01:00
|
|
|
add_member_to_subject @platform
|
2012-10-09 17:23:48 +01:00
|
|
|
end
|
|
|
|
|
2012-10-09 18:07:10 +01:00
|
|
|
def remove_member
|
2012-10-14 13:39:58 +01:00
|
|
|
remove_member_from_subject @platform
|
2012-10-09 18:07:10 +01:00
|
|
|
end
|
|
|
|
|
2012-10-10 16:13:14 +01:00
|
|
|
def clone
|
2012-10-10 17:31:22 +01:00
|
|
|
platform_params = params[:platform] || {}
|
|
|
|
platform_params[:owner] = current_user
|
2012-10-10 16:13:14 +01:00
|
|
|
@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'
|
2012-10-10 16:13:14 +01:00
|
|
|
else
|
2012-10-11 16:36:14 +01:00
|
|
|
render_validation_error @platform, 'Platform has not been cloned'
|
2012-10-10 16:13:14 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def clear
|
|
|
|
@platform.clear
|
2012-10-11 16:36:14 +01:00
|
|
|
render_json_response @platform, 'Platform has been cleared successfully'
|
2012-10-10 16:13:14 +01:00
|
|
|
end
|
|
|
|
|
2012-10-10 16:43:14 +01:00
|
|
|
def destroy
|
2012-10-14 13:39:58 +01:00
|
|
|
destroy_subject @platform
|
2012-10-10 16:43:14 +01:00
|
|
|
end
|
|
|
|
|
2012-08-17 14:40:57 +01:00
|
|
|
end
|