Skip to content

Permission view block helpers

kristianmandrup edited this page Jan 15, 2011 · 5 revisions

Cream comes with a set of view block helpers to make it easy to guard blocks of view logic and only execute that block for Users that match certain role criteria.

for_any_user

for_any_user :logged_in do
  ...
end

The valid options arguments are :signed_in_, :_logged_in, :signed_out, :logged_out and negations with prefix :not_xxxx
You can supply this as either a symbol or a hash (see below)

These argument options also apply for the not_for_any_user method

not_for_any_user

not_for_any_user :logged_in do
  ...
end

not_for_any_user :logged_out => true do
  ...
end

for_role

for_role :editor do
...
end

not_for_role

not_for_role :editor do
...
end

for_roles

for_roles :editor, :admin do
...
end

not_for_roles

not_for_roles :editor, :admin do
...
end

Role group helpers

  • for_user_in_group
  • for_user_in_groups
  • for_user_in_any_group

All with symmetric negation method using prefix: not_xxxx

for_user_in_group :admins do
  ...
end

for_user_in_any_group :admins, :editors do
  ...
end

not_for_user_in_any_group :publishers, :editors do
  ...
end

Area helpers

Cream also comes with some helpers

area_for_roles

area_for_roles :editor, :admin do
...
end

Results in the following ouput if the user has any of the roles listed

<div class="special">
...
</div>

You can also specify a special class option.

area_for_roles :editor, :admin, :class => 'admin' do
...
end

Results in the following ouput if the user has any of the roles listed

<div class="admin">
...
</div>

There are also the methods: area_not_for_roles, area_for_role, area_not_for_role

Host block helpers

Cream also comes with a few helpers to help guard certain areas for “development eyes only” (localhost) and others for public users.

for_localhost

Reverse of for_public

for_localhost do
...
end

for_public

Reverse of for_localhost

for_public do
...
end

Only shows it for request coming in from public users (non-localhost)