Commit e90aebe2 authored by Andrew8xx8's avatar Andrew8xx8

Personal snippets controlelr refactored

parent 9bd1983e
class SnippetsController < ProjectResourceController class SnippetsController < ApplicationController
before_filter :module_enabled
before_filter :snippet, only: [:show, :edit, :destroy, :update, :raw] 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 # Allow modify snippet
before_filter :authorize_modify_snippet!, only: [:edit, :update] before_filter :authorize_modify_snippet!, only: [:edit, :update]
...@@ -17,22 +10,39 @@ class SnippetsController < ProjectResourceController ...@@ -17,22 +10,39 @@ class SnippetsController < ProjectResourceController
respond_to :html respond_to :html
def index 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 end
def new def new
@snippet = @project.snippets.new @snippet = PersonalSnippet.new
end end
def create def create
@snippet = @project.snippets.new(params[:snippet]) @snippet = PersonalSnippet.new(params[:personal_snippet])
@snippet.author = current_user @snippet.author = current_user
@snippet.save @snippet.save
if @snippet.valid? if @snippet.valid?
redirect_to [@project, @snippet] redirect_to snippet_path(@snippet)
else else
respond_with(@snippet) respond_with @snippet
end end
end end
...@@ -40,27 +50,24 @@ class SnippetsController < ProjectResourceController ...@@ -40,27 +50,24 @@ class SnippetsController < ProjectResourceController
end end
def update def update
@snippet.update_attributes(params[:snippet]) @snippet.update_attributes(params[:personal_snippet])
if @snippet.valid? if @snippet.valid?
redirect_to [@project, @snippet] redirect_to snippet_path(@snippet)
else else
respond_with(@snippet) respond_with @snippet
end end
end end
def show def show
@note = @project.notes.new(noteable: @snippet)
@target_type = :snippet
@target_id = @snippet.id
end end
def destroy 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 @snippet.destroy
redirect_to project_snippet_path(@project) redirect_to snippets_path
end end
def raw def raw
...@@ -75,18 +82,14 @@ class SnippetsController < ProjectResourceController ...@@ -75,18 +82,14 @@ class SnippetsController < ProjectResourceController
protected protected
def snippet def snippet
@snippet ||= @project.snippets.find(params[:id]) @snippet ||= PersonalSnippet.find(params[:id])
end end
def authorize_modify_snippet! 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 end
def authorize_admin_snippet! def authorize_admin_snippet!
return render_404 unless can?(current_user, :admin_snippet, @snippet) return render_404 unless can?(current_user, :admin_personal_snippet, @snippet)
end
def module_enabled
return render_404 unless @project.snippets_enabled
end end
end end
...@@ -8,7 +8,7 @@ class Ability ...@@ -8,7 +8,7 @@ class Ability
when "Issue" then issue_abilities(user, subject) when "Issue" then issue_abilities(user, subject)
when "Note" then note_abilities(user, subject) when "Note" then note_abilities(user, subject)
when "ProjectSnippet" then project_snippet_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 "MergeRequest" then merge_request_abilities(user, subject)
when "Group", "Namespace" then group_abilities(user, subject) when "Group", "Namespace" then group_abilities(user, subject)
when "UserTeam" then user_team_abilities(user, subject) when "UserTeam" then user_team_abilities(user, subject)
...@@ -135,8 +135,7 @@ class Ability ...@@ -135,8 +135,7 @@ class Ability
rules.flatten rules.flatten
end end
[:issue, :note, :project_snippet, :personal_snippet, :merge_request].each do |name|
[:issue, :note, :project_snippet, :snippet, :merge_request].each do |name|
define_method "#{name}_abilities" do |user, subject| define_method "#{name}_abilities" do |user, subject|
if subject.author == user 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