
Security News
Rspack Introduces Rslint, a TypeScript-First Linter Written in Go
Rspack launches Rslint, a fast TypeScript-first linter built on typescript-go, joining in on the trend of toolchains creating their own linters.
gem install may_may
MayMay automatically restricts access to actions on all controllers and then allows you to specify permitted actions. Because an action is restricted by default, you will never accidentally allow all users access to a new action that should have been restricted. Permitted actions are all listed in one place (a May model) for ease of maintenance.
Simplify your views. Show/hide view elements based on intended action instead of current user's roles:
<% may? (:edit, :people) do %>
<%= link_to 'only visible if user may edit people', edit_people_path %>
<% end %>
# in app/models/may.rb
class May
May.permission_setup do
controller :people do
may :index # anyone can execute the index action
may :create, :only => [:administrator] # only the administrator may create a person
may :edit, :except => [:guest] # everyone except guests may edit a person
may [:new, :update] do
# return true to allow these actions
# block executed in controller's scope
end
end # :destroy was never mentioned and so is not permitted for anyone
end
end
Here is a simple example setup:
# in app/models/role.rb
class Role
# roles table should have a "name" column
end
# in app/models/user_role.rb
class UserRole
has_many :roles
end
# in app/models/user.rb
class User
has_many :user_roles
has_many :roles, through: :user_roles
def role_names
roles.map {|role| role.name.to_sym }
end
end
# in app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
def current_user
@current_user ||= User.find(session[:user_id]) rescue nil
end
def current_user=(value)
value ? session[:user_id] = value.id : session.delete(:id)
@current_user = value
end
end
The important thing is to return an array of roles that can be matched against the roles specified in your May model. Your array may contain whatever types you'd like -- strings, symbols, models. Symbols are probably most readable, however, hence the example above.
You could instead define permissions within the controller itself:
class UsersController
may :index, only: [:some, :roles]
may :show, method: [:may_show?]
end
MayMay adds a few methods to ActionController::Base:
*** may
(class method)
Define permissions in your controller instead of May model. Shortcut for May.may(self, ...)
Usage:
class SomeController < ApplicationController
may :index, except: [:this_role]
end
*** current_roles
Returns current_user.role_names
or empty array if current_user
returns nil or is missing
*** has_role?
Check current_roles
for a specific role.
Usage: if has_role? :some_role
*** may? (helper method)
Check for permission to perform an action.
Usage: if may? :action_name, :controller_name
MIT-LICENSE
FAQs
Unknown package
We found that may_may demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Rspack launches Rslint, a fast TypeScript-first linter built on typescript-go, joining in on the trend of toolchains creating their own linters.
Security News
Hacker Demonstrates How Easy It Is To Steal Data From Popular Password Managers
Security News
Oxlint’s new preview brings type-aware linting powered by typescript-go, combining advanced TypeScript rules with native-speed performance.