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 6ab4616..b66e978 100644 --- a/app/controllers/wikis_controller.rb +++ b/app/controllers/wikis_controller.rb @@ -6,13 +6,18 @@ 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 # 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/models/user.rb b/app/models/user.rb index 06596d9..e89c077 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -4,9 +4,14 @@ class User < ActiveRecord::Base devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable + has_many :collaborations has_many :wikis, through: :collaborations after_initialize :init + def role?(base_role) + role == base_role.to_s + end + def admin? role == 'admin' end @@ -26,5 +31,9 @@ def init def upgrade self.role = 'premium' end + + def current_user? + current_user = self + end end 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..f28bfeb 100644 --- a/app/policies/wiki_policy.rb +++ b/app/policies/wiki_policy.rb @@ -1,3 +1,53 @@ class WikiPolicy < ApplicationPolicy + 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) + #collab_wikis = Wiki.where(id: collaborations.pluck(:wiki_id)) + #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 + + 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/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/pages/index.html.haml b/app/views/pages/index.html.haml index 1b4c4e4..263c547 100644 --- a/app/views/pages/index.html.haml +++ b/app/views/pages/index.html.haml @@ -20,4 +20,7 @@ = 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]) +-#= 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]) diff --git a/app/views/wikis/_form.html.haml b/app/views/wikis/_form.html.haml index 9ec4431..952c0e0 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 + - if current_user.role?(:admin) || current_user.role?(:premium) + .field + = f.label :is_private + = f.check_box :is_private + .actions - = f.submit 'Save' + = f.submit 'Save' \ No newline at end of file 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?' }