From f8c00b48c395d0c63d71cd957b572218fb495305 Mon Sep 17 00:00:00 2001 From: Tyler Dary Date: Sat, 20 Dec 2014 16:10:19 +0000 Subject: [PATCH 1/5] removed private checkbox for non-premium users --- app/models/user.rb | 4 ++++ app/views/wikis/_form.html.haml | 12 +++++++----- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/app/models/user.rb b/app/models/user.rb index 06596d9..497d304 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -7,6 +7,10 @@ class User < ActiveRecord::Base has_many :wikis, through: :collaborations after_initialize :init + def role?(base_role) + role == base_role.to_s + end + def admin? role == 'admin' end diff --git a/app/views/wikis/_form.html.haml b/app/views/wikis/_form.html.haml index 9ec4431..ad07a74 100644 --- a/app/views/wikis/_form.html.haml +++ b/app/views/wikis/_form.html.haml @@ -12,8 +12,10 @@ .field = f.label :user_id = f.number_field :user_id - .field - = f.label :is_private - = f.check_box :is_private - .actions - = f.submit 'Save' + - if current_user.role?(:admin) || current_user.role?(:premium) + .field + = f.label :is_private + = f.check_box :is_private + + .actions + = f.submit 'Save' \ No newline at end of file From 53f0d73bed2d2493dcc1bb79a6e2840bef73b0d1 Mon Sep 17 00:00:00 2001 From: Tyler Dary Date: Sat, 27 Dec 2014 16:04:45 +0000 Subject: [PATCH 2/5] wikis view shows correct wikis --- app/controllers/wikis_controller.rb | 3 ++- app/models/user.rb | 1 + app/models/wiki.rb | 1 + app/policies/wiki_policy.rb | 35 +++++++++++++++++++++++++++++ app/views/wikis/index.html.haml | 3 ++- 5 files changed, 41 insertions(+), 2 deletions(-) diff --git a/app/controllers/wikis_controller.rb b/app/controllers/wikis_controller.rb index 6ab4616..f46c41b 100644 --- a/app/controllers/wikis_controller.rb +++ b/app/controllers/wikis_controller.rb @@ -6,7 +6,8 @@ class WikisController < ApplicationController def index #@user = current_user #@wikis = Wiki.where(user_id: params[:user_id]) - @wikis = Wiki.where(user_id: current_user) + #@wikis = Wiki.where(user_id: current_user) + @wikis = policy_scope(Wiki) end # GET /wikis/1 diff --git a/app/models/user.rb b/app/models/user.rb index 497d304..fd63818 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -4,6 +4,7 @@ class User < ActiveRecord::Base devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable + has_many :collaborations has_many :wikis, through: :collaborations after_initialize :init diff --git a/app/models/wiki.rb b/app/models/wiki.rb index d09bba2..63e8382 100644 --- a/app/models/wiki.rb +++ b/app/models/wiki.rb @@ -1,6 +1,7 @@ class Wiki < ActiveRecord::Base has_many :pages belongs_to :user + has_many :collaborations has_many :users, through: :collaborations end diff --git a/app/policies/wiki_policy.rb b/app/policies/wiki_policy.rb index 735cb6a..f895ab6 100644 --- a/app/policies/wiki_policy.rb +++ b/app/policies/wiki_policy.rb @@ -1,3 +1,38 @@ class WikiPolicy < ApplicationPolicy +class Scope + attr_reader :user, :scope + + def initialize(user, scope) + @user = user + @scope = scope + end + + def resolve + wikis = [] + if user.role?(:admin) + wikis = scope.all # if the user is an admin, show them all the wikis + elsif user.role?(:premium) + collaborations = Collaboration.where(:user_id => user) + collab_wikis = Wiki.where(id: collaborations.pluck(:wiki_id)) + all_wikis = scope.all + all_wikis.each do |wiki| + if !wiki.is_private || wiki.user_id == user.id || collab_wikis.include?(wiki) + wikis << wiki # if the user is premium, only show them public wikis, or that private wikis they created, or private wikis they are a collaborator on + end + end + else # this is the lowly standard user + collaborations = Collaboration.where(:user_id => user) + collab_wikis = Wiki.where(id: collaborations.pluck(:wiki_id)) + all_wikis = scope.all + wikis = [] + all_wikis.each do |wiki| + if !wiki.is_private? || wiki.user_id == user.id || collab_wikis.include?(wiki) + wikis << wiki # only show standard users public wikis and private wikis they are a collaborator on + end + end + end + wikis # return the wikis array we've built up + end + end end diff --git a/app/views/wikis/index.html.haml b/app/views/wikis/index.html.haml index 5b3cdd7..cf24035 100644 --- a/app/views/wikis/index.html.haml +++ b/app/views/wikis/index.html.haml @@ -5,7 +5,7 @@ %th Title %th User %th Is private - %th + %th Wiki ID %th %th @@ -14,6 +14,7 @@ %td= wiki.title %td= wiki.user_id %td= wiki.is_private + %td= wiki.id %td= link_to 'Show', wiki_pages_path(wiki) %td= link_to 'Edit', edit_wiki_path(wiki) %td= link_to 'Destroy', wiki, :method => :delete, :data => { :confirm => 'Are you sure?' } From bf891257e7bd3e163d7b85be5f43e4cc30bb4a09 Mon Sep 17 00:00:00 2001 From: Tyler Dary Date: Sat, 3 Jan 2015 16:29:25 +0000 Subject: [PATCH 3/5] updated collaborations links --- app/controllers/wikis_controller.rb | 4 ++++ app/policies/wiki_policy.rb | 11 +++++++++++ app/views/collaborations/save.html.haml | 18 +++++++++++++++++- app/views/wikis/_form.html.haml | 4 ++-- 4 files changed, 34 insertions(+), 3 deletions(-) diff --git a/app/controllers/wikis_controller.rb b/app/controllers/wikis_controller.rb index f46c41b..3ae15c9 100644 --- a/app/controllers/wikis_controller.rb +++ b/app/controllers/wikis_controller.rb @@ -14,6 +14,10 @@ def index # GET /wikis/1.json def show @wiki = Wiki.find(params[:id]) + #@authorized_wiki = policy_scope(Wiki) + #if @authorized_wiki.include?(@wiki) + #authorize @wiki + #end end # GET /wikis/new diff --git a/app/policies/wiki_policy.rb b/app/policies/wiki_policy.rb index f895ab6..0c97560 100644 --- a/app/policies/wiki_policy.rb +++ b/app/policies/wiki_policy.rb @@ -1,4 +1,15 @@ class WikiPolicy < ApplicationPolicy + +def show? + scope.where(:id => record.id).exists? + #wiki_user = Wiki.find(record.id).user + #current_wiki = Wiki.find(params[:id]) + #collaborations = Collaboration.where(:user_id => user) + #collab_wikis = Wiki.where(id: collaborations.pluck(:wiki_id)) + #collab_wikis.include?(current_wiki) + end + + class Scope attr_reader :user, :scope diff --git a/app/views/collaborations/save.html.haml b/app/views/collaborations/save.html.haml index e4b2e25..82b154c 100644 --- a/app/views/collaborations/save.html.haml +++ b/app/views/collaborations/save.html.haml @@ -1 +1,17 @@ -= @collabs \ No newline at end of file +%p You now have the following collaborators added. += @collabs +%h1{:align => "center"} My Collaborations +%table + %thead + %tr + %th Name + %th Email + + %tbody + - @collabs.each do |collabs| + %tr + %td= User.find(collabs).name + %td= User.find(collabs).email + +%p= link_to 'Edit Collaborations', wiki_collaborations_path +%p= link_to 'Back to Wiki', wiki_pages_path \ No newline at end of file diff --git a/app/views/wikis/_form.html.haml b/app/views/wikis/_form.html.haml index ad07a74..952c0e0 100644 --- a/app/views/wikis/_form.html.haml +++ b/app/views/wikis/_form.html.haml @@ -17,5 +17,5 @@ = f.label :is_private = f.check_box :is_private - .actions - = f.submit 'Save' \ No newline at end of file + .actions + = f.submit 'Save' \ No newline at end of file From c4d6e8fa2ea6d81cd07c60309154d115cdb0fc6d Mon Sep 17 00:00:00 2001 From: Tyler Dary Date: Mon, 5 Jan 2015 02:30:01 +0000 Subject: [PATCH 4/5] working on updating collaboration link --- app/models/user.rb | 4 ++++ app/views/pages/index.html.haml | 3 ++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/app/models/user.rb b/app/models/user.rb index fd63818..e89c077 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -31,5 +31,9 @@ def init def upgrade self.role = 'premium' end + + def current_user? + current_user = self + end end diff --git a/app/views/pages/index.html.haml b/app/views/pages/index.html.haml index 1b4c4e4..c49115e 100644 --- a/app/views/pages/index.html.haml +++ b/app/views/pages/index.html.haml @@ -20,4 +20,5 @@ = link_to 'New Page', new_wiki_page_path(params[:wiki_id]) = link_to 'My Wikis', wikis_path -= link_to 'Add Collaborators', wiki_collaborations_path(params[:wiki_id]) +-#- if @current.current_user?(Wiki.find(params[:wiki_id]).user) +-#= link_to 'Add Collaborators', wiki_collaborations_path(params[:wiki_id]) From 6fda654ecde4eff8dc88337c1fa02cbab487eeff Mon Sep 17 00:00:00 2001 From: "Phil Spitler (PRS)" Date: Mon, 5 Jan 2015 14:54:19 -0500 Subject: [PATCH 5/5] working on policy stuff --- app/controllers/pages_controller.rb | 1 + app/controllers/wikis_controller.rb | 2 +- app/policies/wiki_policy.rb | 76 +++++++++++++++-------------- app/views/pages/index.html.haml | 2 + 4 files changed, 44 insertions(+), 37 deletions(-) diff --git a/app/controllers/pages_controller.rb b/app/controllers/pages_controller.rb index baf873b..411a5d0 100644 --- a/app/controllers/pages_controller.rb +++ b/app/controllers/pages_controller.rb @@ -4,6 +4,7 @@ class PagesController < ApplicationController # GET /pages # GET /pages.json def index + @wiki = Wiki.find(params[:wiki_id]) @pages = Page.all.where(wiki_id: params[:wiki_id]) end diff --git a/app/controllers/wikis_controller.rb b/app/controllers/wikis_controller.rb index 3ae15c9..b66e978 100644 --- a/app/controllers/wikis_controller.rb +++ b/app/controllers/wikis_controller.rb @@ -16,7 +16,7 @@ def show @wiki = Wiki.find(params[:id]) #@authorized_wiki = policy_scope(Wiki) #if @authorized_wiki.include?(@wiki) - #authorize @wiki + authorize @wiki #end end diff --git a/app/policies/wiki_policy.rb b/app/policies/wiki_policy.rb index 0c97560..f28bfeb 100644 --- a/app/policies/wiki_policy.rb +++ b/app/policies/wiki_policy.rb @@ -1,7 +1,8 @@ class WikiPolicy < ApplicationPolicy -def show? - scope.where(:id => record.id).exists? + def show? + user.id == record.user_id or Collaboration.where(:user_id => user.id).length > 0 + #scope.where(:id => record.id).exists? #wiki_user = Wiki.find(record.id).user #current_wiki = Wiki.find(params[:id]) #collaborations = Collaboration.where(:user_id => user) @@ -9,41 +10,44 @@ def show? #collab_wikis.include?(current_wiki) end + def add_collaborators? + user.premium? and record.is_private? + end + + class Scope + attr_reader :user, :scope + + def initialize(user, scope) + @user = user + @scope = scope + end -class Scope - attr_reader :user, :scope - - def initialize(user, scope) - @user = user - @scope = scope - end - - def resolve - wikis = [] - if user.role?(:admin) - wikis = scope.all # if the user is an admin, show them all the wikis - elsif user.role?(:premium) - collaborations = Collaboration.where(:user_id => user) - collab_wikis = Wiki.where(id: collaborations.pluck(:wiki_id)) - all_wikis = scope.all - all_wikis.each do |wiki| - if !wiki.is_private || wiki.user_id == user.id || collab_wikis.include?(wiki) - wikis << wiki # if the user is premium, only show them public wikis, or that private wikis they created, or private wikis they are a collaborator on - end - end - else # this is the lowly standard user - collaborations = Collaboration.where(:user_id => user) - collab_wikis = Wiki.where(id: collaborations.pluck(:wiki_id)) - all_wikis = scope.all - wikis = [] - all_wikis.each do |wiki| - if !wiki.is_private? || wiki.user_id == user.id || collab_wikis.include?(wiki) - wikis << wiki # only show standard users public wikis and private wikis they are a collaborator on - end + def resolve + wikis = [] + if user.role?(:admin) + wikis = scope.all # if the user is an admin, show them all the wikis + elsif user.role?(:premium) + collaborations = Collaboration.where(:user_id => user) + collab_wikis = Wiki.where(id: collaborations.pluck(:wiki_id)) + all_wikis = scope.all + all_wikis.each do |wiki| + if !wiki.is_private || wiki.user_id == user.id || collab_wikis.include?(wiki) + wikis << wiki # if the user is premium, only show them public wikis, or that private wikis they created, or private wikis they are a collaborator on end - end - wikis # return the wikis array we've built up - end - end + end + else # this is the lowly standard user + collaborations = Collaboration.where(:user_id => user) + collab_wikis = Wiki.where(id: collaborations.pluck(:wiki_id)) + all_wikis = scope.all + wikis = [] + all_wikis.each do |wiki| + if !wiki.is_private? || wiki.user_id == user.id || collab_wikis.include?(wiki) + wikis << wiki # only show standard users public wikis and private wikis they are a collaborator on + end + end + end + wikis # return the wikis array we've built up + end + end end diff --git a/app/views/pages/index.html.haml b/app/views/pages/index.html.haml index c49115e..263c547 100644 --- a/app/views/pages/index.html.haml +++ b/app/views/pages/index.html.haml @@ -20,5 +20,7 @@ = link_to 'New Page', new_wiki_page_path(params[:wiki_id]) = link_to 'My Wikis', wikis_path +-#= debug(Pundit.policy(User.first, Wiki.first).add_collaborators?) += link_to 'Add Collaborators', wiki_collaborations_path(params[:wiki_id]) if Pundit.policy(current_user, @wiki).add_collaborators? -#- if @current.current_user?(Wiki.find(params[:wiki_id]).user) -#= link_to 'Add Collaborators', wiki_collaborations_path(params[:wiki_id])