Class: Users::Controller::UserGroups

Inherits:
Zen::Controller::AdminController show all
Defined in:
lib/zen/package/users/lib/users/controller/user_groups.rb

Overview

User groups allow you to group types of users together and assign permissions to the entire group of users without having to modify each individual user.

User groups can be managed by going to /admin/user-groups. This page will show an overview of all existing groups or a message saying no groups have been added yet.

User Groups

Editing a user group can be done by clicking on the name of the group, creating a new one can be done by clicking the button "New group". When creating or editing a group you'll be presented with the form shown in the images below.

Edit Group Group Permissions

In this form you can fill in the following fields:

  • Name (required): the name of the user group.
  • Slug: a URL friendly version of the name. If no name is specified one will be generated automatically.
  • Super group (required): when set to "Yes" all users that are assigned to this group will have access to everything regardless of their individual settings.
  • Description: a description of the user group.

Besides these fields you can also specify all the permissions o the user group similar to how they're managed for individual users. Note that user specific rules will only overwrite group based rules if a group blocks something while a user specific rules allows something. Simply said, rules are added to the list but aren't removed based on their source.

Used Permissions

This controller uses the following permissions:

  • show_user_group
  • edit_user_group
  • new_user_group
  • delete_user_group

Events

All events in this controller receive an instance of Model::UserGroup. Just like other controllers the event after_delete_user_group will receive a user group that has already been destroyed using #destroy().

Since:

Instance Method Summary (collapse)

Instance Method Details

- (Object) delete

Deletes all specified user groups.

Since:

  • 0.1



216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/zen/package/users/lib/users/controller/user_groups.rb', line 216

def delete
  authorize_user!(:delete_user_group)

  if !request.params['user_group_ids'] \
  or request.params['user_group_ids'].empty?
    message(:error, lang('user_groups.errors.no_delete'))
    redirect_referrer
  end

  request.params['user_group_ids'].each do |id|
    group = ::Users::Model::UserGroup[id]

    next if group.nil?
    Zen::Event.call(:before_delete_user_group, group)

    begin
      group.destroy
    rescue => e
      Ramaze::Log.error(e.inspect)
      message(:error, lang('user_groups.errors.delete') % id)

      redirect_referrer
    end

    Zen::Event.call(:after_delete_user_group, group)
  end

  message(:success,  lang('user_groups.success.delete'))
  redirect_referrer
end

- (Object) edit(id)

Edit an existing user group.

Parameters:

  • id (Fixnum)

    The ID of the user group to edit.

Since:

  • 0.1



109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/zen/package/users/lib/users/controller/user_groups.rb', line 109

def edit(id)
  authorize_user!(:edit_user_group)

  set_breadcrumbs(
    UserGroups.a(lang('user_groups.titles.index'), :index),
    lang('user_groups.titles.edit')
  )

  @user_group  = flash[:form_data] || validate_user_group(id)
  @permissions = @user_group.permissions.map { |p| p.permission.to_sym }

  render_view(:form)
end

- (Object) index

Show an overview of all user groups and allow the current user to manage these groups

Since:

  • 0.1



89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/zen/package/users/lib/users/controller/user_groups.rb', line 89

def index
  authorize_user!(:show_user_group)

  set_breadcrumbs(lang('user_groups.titles.index'))

  @user_groups = search do |query|
    ::Users::Model::UserGroup.search(query).order(:id.asc)
  end

  @user_groups ||= ::Users::Model::UserGroup.order(:id.asc)
  @user_groups   = paginate(@user_groups)
end

- (Object) new

Create a new user group.

Since:

  • 0.1



129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/zen/package/users/lib/users/controller/user_groups.rb', line 129

def new
  authorize_user!(:new_user_group)

  set_breadcrumbs(
    UserGroups.a(lang('user_groups.titles.index'), :index),
    lang('user_groups.titles.new')
  )

  @user_group = flash[:form_data] || ::Users::Model::UserGroup.new

  render_view(:form)
end

- (Object) save

Saves or creates a new user group based on the POST data and a field named 'id'.

Since:

  • 0.1



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/zen/package/users/lib/users/controller/user_groups.rb', line 154

def save
  post = request.subset(:id, :name, :slug, :description, :super_group)

  if post['id'] and !post['id'].empty?
    authorize_user!(:edit_user_group)

    user_group   = validate_user_group(post['id'])
    save_action  = :save
    before_event = :before_edit_user_group
    after_event  = :after_edit_user_group
  else
    authorize_user!(:new_user_group)

    user_group   = ::Users::Model::UserGroup.new
    save_action  = :new
    before_event = :before_new_user_group
    after_event  = :after_new_user_group
  end

  post.delete('id')

  success = lang("user_groups.success.#{save_action}")
  error   = lang("user_groups.errors.#{save_action}")

  begin
    post.each { |k, v| user_group.send("#{k}=", v) }
    Zen::Event.call(before_event, user_group)

    user_group.save
  rescue => e
    Ramaze::Log.error(e.inspect)
    message(:error, error)

    flash[:form_data]   = user_group
    flash[:form_errors] = user_group.errors

    redirect_referrer
  end

  if user_authorized?(:edit_permission)
    update_permissions(
      :user_group_id,
      user_group.id,
      request.params['permissions'] || [],
      user_group.permissions.map { |p| p.permission }
    )
  end

  Zen::Event.call(after_event, user_group)

  message(:success, success)
  redirect(UserGroups.r(:edit, user_group.id))
end