Sha256: 4e099b7bae705f0d579f4c475b36d1bd834ea6fadd176215a03a7044ac57a9d2

Contents?: true

Size: 1.17 KB

Versions: 1

Compression:

Stored size: 1.17 KB

Contents

module Thredded
  class TopicUserPermissions
    attr_reader :topic, :user, :user_details, :messageboard

    def initialize(topic, user, user_details)
      @topic = topic
      @messageboard = topic.messageboard
      @user = user
      @user_details = user_details || UserDetail.new
    end

    def creatable?
      member? || messageboard_restrictions_allow?
    end

    def adminable?
      superadmin? || administrates_messageboard?
    end

    def editable?
      superadmin? || administrates_messageboard? || started_by_user?
    end

    def readable?
      MessageboardUserPermissions.new(messageboard, user).readable?
    end

    private

    def messageboard_restrictions_allow?
      user.valid? &&
        (
          messageboard.public? ||
          messageboard.restricted_to_logged_in? ||
          messageboard.posting_for_logged_in?
        )
    end

    def member?
      user.valid? && messageboard.has_member?(user)
    end

    def superadmin?
      user_details.superadmin?
    end

    def started_by_user?
      topic.user_id == user.id
    end

    def administrates_messageboard?
      user.valid? && messageboard.member_is_a?(user, 'admin')
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
thredded-0.0.12 lib/thredded/topic_user_permissions.rb