Note that there are some explanatory texts on larger screens.

plurals
  1. POFinding forums based on a user's permissions
    primarykey
    data
    text
    <p>I'm implementing a forum system called rBoard. Code is viewable at <a href="http://github.com/radar/rboard" rel="nofollow noreferrer">http://github.com/radar/rboard</a>. I've reached an impasse with the permissions code that I've been trying to implement and I've decided to turn to the all-knowing, ever-caring Stack Overflow to solve this issue.</p> <p>Relevant information is thusly:</p> <p><strong>Category model</strong></p> <pre><code>class Category &lt; ActiveRecord::Base has_many :permissions has_many :groups, :through =&gt; :permissions has_many :forums end </code></pre> <p><strong>Forum model</strong></p> <pre><code>class Forum &lt; ActiveRecord::Base has_many :permissions has_many :groups, :through =&gt; :permissions belongs_to :category end </code></pre> <p><strong>Group model</strong></p> <pre><code>class Group &lt; ActiveRecord::Base has_many :group_users has_many :users, :through =&gt; :group_users belongs_to :owner, :class_name =&gt; "User" end </code></pre> <p><strong>Permission model</strong></p> <pre><code>class Permission &lt; ActiveRecord::Base belongs_to :forum belongs_to :category belongs_to :group end </code></pre> <p><strong>User model</strong> </p> <pre><code>class User &lt; ActiveRecord::Base include Rboard::Permissions has_many :group_users # Note: I am using nested_has_many_through has_many :groups, :through =&gt; :group_users has_many :permissions, :through =&gt; :groups end </code></pre> <p><strong>Permissions module</strong></p> <pre><code>module Rboard::Permissions THINGS = ['forum', 'category'] def self.included(klass) klass.class_eval do # Here we can pass an object to check if the user or any the user's groups # has permissions on that particular option. def overall_permissions(thing = nil) conditions = if thing.nil? THINGS.map do |t| "permissions.#{t}_id " + (thing.nil? ? " IS NULL" : "= #{thing.id}") + " OR permissions.#{t}_id IS NULL" end.join(" AND ") else association = thing.class.to_s.downcase "permissions.#{association}_id = #{thing.id} OR permissions.#{association}_id IS NULL" end permissions.all(:conditions =&gt; conditions) end def can?(action, thing = nil) permissions = overall_permissions(thing) !!permissions.detect { |p| p.send("can_#{action}") } end end end end </code></pre> <p>Hopefully with this you should be able to figure out the fields in the permissions table are like <code>can_see_forum</code> and so on. Extra fields are <code>forum_id</code>, <code>category_id</code> and <code>default</code> (default is currently unused)</p> <p>What I want to know is, how can I find all the forums a group can see? Generally if a forum_id is set, then that permission applies. If there is only one permission for that group without specifying a forum_id or category_id then it is seen to be global to everything. I'm completely at a loss here.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload