Commit e90aebe2 authored by Andrew8xx8's avatar Andrew8xx8

Personal snippets controlelr refactored

parent 9bd1983e
class SnippetsController < ProjectResourceController
before_filter :module_enabled
class SnippetsController < ApplicationController
before_filter :snippet, only: [:show, :edit, :destroy, :update, :raw]
# Allow read any snippet
before_filter :authorize_read_snippet!
# Allow write(create) snippet
before_filter :authorize_write_snippet!, only: [:new, :create]
# Allow modify snippet
before_filter :authorize_modify_snippet!, only: [:edit, :update]
......@@ -17,22 +10,39 @@ class SnippetsController < ProjectResourceController
respond_to :html
def index
@snippets = @project.snippets.fresh.non_expired
@snippets = Snippet.public.fresh.non_expired.page(params[:page]).per(20)
end
def user_index
@user = User.find_by_username(params[:username])
@snippets = @current_user.snippets.fresh.non_expired
@snippets = case params[:scope]
when 'public' then
@snippets.public
when 'private' then
@snippets.private
else
@snippets
end
@snippets = @snippets.page(params[:page]).per(20)
end
def new
@snippet = @project.snippets.new
@snippet = PersonalSnippet.new
end
def create
@snippet = @project.snippets.new(params[:snippet])
@snippet = PersonalSnippet.new(params[:personal_snippet])
@snippet.author = current_user
@snippet.save
if @snippet.valid?
redirect_to [@project, @snippet]
redirect_to snippet_path(@snippet)
else
respond_with(@snippet)
respond_with @snippet
end
end
......@@ -40,27 +50,24 @@ class SnippetsController < ProjectResourceController
end
def update
@snippet.update_attributes(params[:snippet])
@snippet.update_attributes(params[:personal_snippet])
if @snippet.valid?
redirect_to [@project, @snippet]
redirect_to snippet_path(@snippet)
else
respond_with(@snippet)
respond_with @snippet
end
end
def show
@note = @project.notes.new(noteable: @snippet)
@target_type = :snippet
@target_id = @snippet.id
end
def destroy
return access_denied! unless can?(current_user, :admin_snippet, @snippet)
return access_denied! unless can?(current_user, :admin_personal_snippet, @snippet)
@snippet.destroy
redirect_to project_snippet_path(@project)
redirect_to snippets_path
end
def raw
......@@ -75,18 +82,14 @@ class SnippetsController < ProjectResourceController
protected
def snippet
@snippet ||= @project.snippets.find(params[:id])
@snippet ||= PersonalSnippet.find(params[:id])
end
def authorize_modify_snippet!
return render_404 unless can?(current_user, :modify_snippet, @snippet)
return render_404 unless can?(current_user, :modify_personal_snippet, @snippet)
end
def authorize_admin_snippet!
return render_404 unless can?(current_user, :admin_snippet, @snippet)
end
def module_enabled
return render_404 unless @project.snippets_enabled
return render_404 unless can?(current_user, :admin_personal_snippet, @snippet)
end
end
......@@ -8,7 +8,7 @@ class Ability
when "Issue" then issue_abilities(user, subject)
when "Note" then note_abilities(user, subject)
when "ProjectSnippet" then project_snippet_abilities(user, subject)
when "Snippet" then snippet_abilities(user, subject)
when "PersonalSnippet" then personal_snippet_abilities(user, subject)
when "MergeRequest" then merge_request_abilities(user, subject)
when "Group", "Namespace" then group_abilities(user, subject)
when "UserTeam" then user_team_abilities(user, subject)
......@@ -135,8 +135,7 @@ class Ability
rules.flatten
end
[:issue, :note, :project_snippet, :snippet, :merge_request].each do |name|
[:issue, :note, :project_snippet, :personal_snippet, :merge_request].each do |name|
define_method "#{name}_abilities" do |user, subject|
if subject.author == user
[
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment