Skip to content

Latest commit

 

History

History
129 lines (94 loc) · 2.36 KB

TheRoleAPI.md

File metadata and controls

129 lines (94 loc) · 2.36 KB

[ Back to TheRole ]


TheRole API

User

# User's role
@user.role # => Role obj

Is a user Administrator?

@user.admin?                       => true | false

Is a user Moderator?

@user.moderator?(:pages)           => true | false
@user.moderator?(:blogs)           => true | false
@user.moderator?(:articles)        => true | false

Has user got access to rule of section (action of controller)?

@user.has_role?(:pages,    :show)  => true | false
@user.has_role?(:blogs,    :new)   => true | false
@user.has_role?(:articles, :edit)  => true | false

# return true if one of roles is true
@user.any_role?(pages: :show, posts: :show) => true | false

Is user Owner of object?

@user.owner?(@page)                => true | false
@user.owner?(@blog)                => true | false
@user.owner?(@article)             => true | false

Role

# Find a Role by name
@role = Role.with_name(:user)
@role.has?(:pages, :show)       => true | false
@role.moderator?(:pages)        => true | false
@role.admin?                    => true | false

# return true if one of roles is true
@role.any?(pages: :show, posts: :show)     => true | false
@role.any?(pages: [:show], posts: [:show]) => true | false

@role.any?(pages: [:top_secret])                        => true | false
@role.any?(pages: [:show, :top_secret], posts: [:edit]) => true | false

CREATE

# Create a section of rules
@role.create_section(:pages)
# Create rule in section (false value by default)
@role.create_rule(:pages, :index)

READ

@role.to_hash => Hash

# JSON string
@role.to_json => String

# check method
@role.has_section?(:pages) => true | false

UPDATE

# set this rule on
@role.rule_on(:pages, :index)
# set this rule off
@role.rule_off(:pages, :index)
# Incoming hash is true-mask-hash
# All the rules of the Role will be reset to false
# Only rules from true-mask-hash will be set true
new_role_hash = {
  :pages => {
    :index => true,
    :show => true
  }
}

@role.update_role(new_role_hash)

DELETE

# delete a section
@role.delete_section(:pages)

# delete a rule in section
@role.delete_rule(:pages, :show)

[ Back to TheRole ]