From d49922eb630abb73c7faffb650ab97b3c5d94c52 Mon Sep 17 00:00:00 2001 From: Shrey Date: Tue, 26 May 2015 18:12:43 +0530 Subject: [PATCH] Added authorization + issues dashboard --- app/assets/stylesheets/common.scss | 5 +++ app/controllers/issues_controller.rb | 11 +++++- app/helpers/issues_helper.rb | 23 +++++++++++++ app/models/issue.rb | 2 +- app/models/user.rb | 3 ++ app/views/diary_entry/_diary_entry.html.erb | 2 ++ app/views/issues/_reports.html.erb | 11 ++++++ app/views/issues/index.html.erb | 38 +++++++++++++++++++-- app/views/issues/show.html.erb | 31 +++++++++++++++-- config/locales/en-GB.yml | 2 ++ config/locales/en.yml | 2 ++ 11 files changed, 124 insertions(+), 6 deletions(-) create mode 100644 app/views/issues/_reports.html.erb diff --git a/app/assets/stylesheets/common.scss b/app/assets/stylesheets/common.scss index b2b6057be..24538ebfe 100644 --- a/app/assets/stylesheets/common.scss +++ b/app/assets/stylesheets/common.scss @@ -2739,3 +2739,8 @@ input.richtext_title[type="text"] { display: none; } } + +.read-reports { + background: #eee; + opacity: 0.7; +} diff --git a/app/controllers/issues_controller.rb b/app/controllers/issues_controller.rb index 0479e6641..6ca61b4ce 100644 --- a/app/controllers/issues_controller.rb +++ b/app/controllers/issues_controller.rb @@ -1,6 +1,8 @@ class IssuesController < ApplicationController layout "site" + before_action :authorize_web + before_action :check_permission, only: [:index, :show, :resolve,:open,:ignore] before_action :find_issue, only: [:show, :resolve, :reopen, :ignore] def index @@ -71,6 +73,13 @@ class IssuesController < ApplicationController @issue = Issue.find(params[:id]) end + def check_permission + unless @user.administrator? + flash[:error] = t("application.require_admin.not_an_admin") + redirect_to root_path + end + end + def create_new_issue_params params.permit(:reportable_id, :reportable_type, :user_id) end @@ -80,6 +89,6 @@ class IssuesController < ApplicationController end def report_params - params[:report].permit(:details) + params[:report].permit(:details, :user_id) end end diff --git a/app/helpers/issues_helper.rb b/app/helpers/issues_helper.rb index bfb9d25e5..7e9e233df 100644 --- a/app/helpers/issues_helper.rb +++ b/app/helpers/issues_helper.rb @@ -1,2 +1,25 @@ module IssuesHelper + + def reportable_url(reportable) + class_name = reportable.class.name + case class_name + when "DiaryEntry" + link_to reportable.title, :controller => reportable.class.name.underscore, + :action => :view, + :display_name => reportable.user.display_name, + :id => reportable.id + when "User" + link_to reportable.display_name, :controller => reportable.class.name.underscore, + :action => "view", + :display_name => reportable.diary_entry.user.display_name + when "DiaryComment" + link_to "#{reportable.diary_entry.title} Comment id ##{reportable.id}", :controller => reportable.diary_entry.class.name.underscore, + :action => :view, + :display_name => reportable.diary_entry.user.display_name, + :id => reportable.id + + else + nil + end + end end diff --git a/app/models/issue.rb b/app/models/issue.rb index 1726e690f..277ea3569 100644 --- a/app/models/issue.rb +++ b/app/models/issue.rb @@ -2,7 +2,7 @@ class Issue < ActiveRecord::Base belongs_to :reportable, :polymorphic => true has_many :reports validates :reportable_id, :uniqueness => { :scope => [ :reportable_type ] } - belongs_to :user_id + belongs_to :user # Check if more statuses are needed enum status: %w( open ignored resolved ) diff --git a/app/models/user.rb b/app/models/user.rb index a550b9f05..4a36b3e61 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -26,6 +26,9 @@ class User < ActiveRecord::Base has_many :roles, :class_name => "UserRole" + has_many :issues + has_many :reports + scope :visible, -> { where(:status => %w(pending active confirmed)) } scope :active, -> { where(:status => %w(active confirmed)) } scope :identifiable, -> { where(:data_public => true) } diff --git a/app/views/diary_entry/_diary_entry.html.erb b/app/views/diary_entry/_diary_entry.html.erb index 410e13047..efcd2ec47 100644 --- a/app/views/diary_entry/_diary_entry.html.erb +++ b/app/views/diary_entry/_diary_entry.html.erb @@ -31,6 +31,8 @@ <%= link_to t('diary_entry.diary_entry.edit_link'), :action => 'edit', :display_name => diary_entry.user.display_name, :id => diary_entry.id %> <% end %> +
  • <%= link_to 'Report', new_issue_url(reportable_id: diary_entry.id, reportable_type: diary_entry.class.name, user: diary_entry.user.id) %>
  • + <%= if_administrator(:li) do %> <%= link_to t('diary_entry.diary_entry.hide_link'), hide_diary_entry_path(:display_name => diary_entry.user.display_name, :id => diary_entry.id), :method => :post, :data => { :confirm => t('diary_entry.diary_entry.confirm') } %> <% end %> diff --git a/app/views/issues/_reports.html.erb b/app/views/issues/_reports.html.erb new file mode 100644 index 000000000..0a55a59ec --- /dev/null +++ b/app/views/issues/_reports.html.erb @@ -0,0 +1,11 @@ +<% reports.each do |report| %> +
    +
    + <%= user_thumbnail report.user %> + <%= report.details %> +
    + <%= raw(t('Reported by:',:link_user => (link_to h(report.user.display_name), :controller => :user, :action => :view, :display_name => report.user.display_name), :comment_created_at => link_to(l(report.created_at,:format => :friendly)))) %> + on <%= l report.created_at.to_datetime, :format => :long %> +
    +
    +<% end %> diff --git a/app/views/issues/index.html.erb b/app/views/issues/index.html.erb index 83fe41be9..a599abb99 100644 --- a/app/views/issues/index.html.erb +++ b/app/views/issues/index.html.erb @@ -1,2 +1,36 @@ -

    Issues#index

    -

    Find me in app/views/issues/index.html.erb

    +

    <%= notice %>

    + +<% content_for :heading do %> +

    List of existing Issues:

    +<% end %> + + + + + + + + + + + + + + + + + <% @issues.each do |issue| %> + + + + + + + + + + <% end %> + +
    # Issue Type Status Number of Reports Link to instance Reported User
    Issue #<%= issue.id %> <%= issue.reportable_type %> <%= issue.status %> <%= issue.reports.count %> <%= reportable_url(issue.reportable) %><%= link_to issue.user.display_name , :controller => :user, :action => :view,:display_name => issue.user.display_name %><%= link_to "Show Issue", issue %>
    + + diff --git a/app/views/issues/show.html.erb b/app/views/issues/show.html.erb index 1c3b8bb65..d756595cf 100644 --- a/app/views/issues/show.html.erb +++ b/app/views/issues/show.html.erb @@ -1,2 +1,29 @@ -

    Issues#show

    -

    Find me in app/views/issues/show.html.erb

    +<% content_for :heading do %> +

    Issue #<%= @issue.id %>
    Status: <%= @issue. status %>

    +

    Issue against: <%= reportable_url(@issue.reportable) %>

    +

    Issue type: <%= @issue.reportable_type %>

    +

    + + <%= @issue.reports.count %> reports | First reported: <%= l @issue.created_at.to_date, :format => :long %> | <%= "Last resolved at #{l(@issue.resolved_at.to_datetime, :format =>:long)}" if @issue.resolved? %> + +

    +

    <%= link_to "Resolve", resolve_issue_url(@issue), :method => :post if @issue.may_resolve? %>

    +

    <%= link_to "Ignore", ignore_issue_url(@issue), :method => :post if @issue.may_ignore? %>

    +

    <%= link_to "Reopen", reopen_issue_url(@issue), :method => :post if @issue.may_reopen? %>

    +<% end %> + +

    Reports under this issue:

    + +<% if @read_reports.present? %> +
    +

    Read Reports:

    + <%= render 'reports',reports: @read_reports %> +
    +<% end %> + +<% if @unread_reports.any? %> +
    +

    New Reports:

    + <%= render 'reports',reports: @unread_reports %> +
    +<% end %> diff --git a/config/locales/en-GB.yml b/config/locales/en-GB.yml index 101b71e2e..f3f2a6859 100644 --- a/config/locales/en-GB.yml +++ b/config/locales/en-GB.yml @@ -1675,6 +1675,8 @@ en-GB: require_cookies: cookies_needed: You appear to have cookies disabled - please enable cookies in your browser before continuing. + require_admin: + not_an_admin: You need to be an admin to perform that action. require_moderator: not_a_moderator: You need to be a moderator to perform that action. setup_user_auth: diff --git a/config/locales/en.yml b/config/locales/en.yml index 3c9ec7131..9a03a6a7f 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -1624,6 +1624,8 @@ en: application: require_cookies: cookies_needed: "You appear to have cookies disabled - please enable cookies in your browser before continuing." + require_admin: + not_an_admin: You need to be an admin to perform that action. require_moderator: not_a_moderator: "You need to be a moderator to perform that action." setup_user_auth: -- 2.43.2