Class: Api::V1::GroupsController

Inherits:
BaseController
  • Object
show all
Defined in:
app/controllers/api/v1/groups_controller.rb

Instance Method Summary collapse

Instance Method Details

#addObject



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'app/controllers/api/v1/groups_controller.rb', line 87

def add
  begin
    type = params[:type]
    id = params[:id]
    selected = JSON.parse(params[:group_ids])

    assign_to = type.constantize.find(id)
    selected.each do |group_id|
      group = Group.find(group_id)
      case type
      when 'User'
        group.add_user(assign_to)
      when 'SecurityRole'
        group.add_role(assign_to)
      when 'Capability'
        group.add_capability(assign_to)
      end
    end

    render :json => {:success => true, :message => 'Group(s) Added'}
  rescue => ex
    Rails.logger.error ex.message
    Rails.logger.error ex.backtrace.join("\n")

    ExceptionNotifier.notify_exception(ex) if defined? ExceptionNotifier

    render :json => {:success => false, :message => ex.message}
  end
end

#availableObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'app/controllers/api/v1/groups_controller.rb', line 54

def available
  type = params[:type]
  id = params[:id]

  sort = (params[:sort] || 'description').downcase
  sort = 'groups.description' if sort == 'description'
  dir = (params[:dir] || 'asc').downcase
  query_filter = params[:query_filter].strip rescue nil

  statement = id.blank? ? Group : type.constantize.find(id).groups_not

  statement = (params[:query_filter].blank? ? statement : statement.where("UPPER(groups.description) LIKE UPPER('%#{query_filter}%')"))
  available = statement.paginate(:page => page, :per_page => per_page, :order => "#{sort} #{dir}")

  render :json => {:total_count => statement.count, :groups => available.map { |group| group.to_data_hash }}
end

#createObject



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'app/controllers/api/v1/groups_controller.rb', line 147

def create
  begin
    ActiveRecord::Base.connection.transaction do
      group = Group.create(description: params[:description].strip)

      render json: {success: true, security_role: group.to_data_hash}
    end
  rescue ActiveRecord::RecordInvalid => invalid
    Rails.logger.error invalid.record.errors

    message = "<ul>"
    invalid.record.errors.collect do |e, m|
      message << "<li>#{e} #{m}</li>"
    end
    message << "</ul>"

    render :json => {:success => false, :message => message}
  rescue StandardError => ex
    Rails.logger.error ex.message
    Rails.logger.error ex.backtrace.join("\n")

    ExceptionNotifier.notify_exception(ex) if defined? ExceptionNotifier

    render :json => {:success => false, :message => 'Error creating Group'}
  end
end

#destroyObject



204
205
206
207
208
# File 'app/controllers/api/v1/groups_controller.rb', line 204

def destroy
  group = Group.find(params[:id])

  render json: {success: group.destroy}
end

#effective_securityObject



210
211
212
213
214
215
216
217
218
219
220
221
# File 'app/controllers/api/v1/groups_controller.rb', line 210

def effective_security
  begin
    render :json => {:success => true, :capabilities => Group.find(params[:id]).class_capabilities_to_hash}
  rescue => ex
    Rails.logger.error ex.message
    Rails.logger.error ex.backtrace.join("\n")

    ExceptionNotifier.notify_exception(ex) if defined? ExceptionNotifier

    render :inline => {:success => false, :message => ex.message}
  end
end

#indexObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'app/controllers/api/v1/groups_controller.rb', line 5

def index
  if params[:user_id].present?
    groups = User.find(params[:user_id]).groups
  else
    groups = Group
  end

  respond_to do |format|
    format.json do
      query = params[:query]
      sort_hash = params[:sort].blank? ? {} : Hash.symbolize_keys(JSON.parse(params[:sort]).first)
      sort = sort_hash[:property] || 'description'
      dir = sort_hash[:direction] || 'ASC'
      limit = params[:limit]
      start = params[:start]

      if query
        group_role_tbl = Group.arel_table
        groups = groups.where(group_role_tbl[:description].matches("%#{query}%"))

        total_count = groups.count
        groups = groups.order("#{sort} #{dir}")
      else
        total_count = groups.count
        groups = groups.order("#{sort} #{dir}")
      end

      if limit and start
        groups = groups.limit(limit).offset(start)
      end

      render json: {success: true, total_count: total_count, groups: groups.collect{|group| group.to_data_hash}}
    end
    format.tree do
      nodes = [].tap do |nodes|
        groups.all.each do |group|
          nodes.push({
                       leaf: true,
                       internal_identifier: group.id,
                       text: group.description
          })
        end
      end

      render json: {success: true, groups: nodes}
    end
  end
end

#removeObject



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'app/controllers/api/v1/groups_controller.rb', line 117

def remove
  begin
    type = params[:type]
    id = params[:id]
    selected = JSON.parse(params[:group_ids])

    assign_to = type.constantize.find(id)
    selected.each do |group_id|
      group = Group.find(group_id)
      case type
      when 'User'
        group.remove_user(assign_to)
      when 'SecurityRole'
        group.remove_role(assign_to)
      when 'Capability'
        group.remove_capability(assign_to)
      end
    end

    render :json => {:success => true, :message => 'Group(s) Removed'}
  rescue => ex
    Rails.logger.error ex.message
    Rails.logger.error ex.backtrace.join("\n")

    ExceptionNotifier.notify_exception(ex) if defined? ExceptionNotifier

    render :json => {:success => false, :message => ex.message}
  end
end

#selectedObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'app/controllers/api/v1/groups_controller.rb', line 71

def selected
  type = params[:type]
  id = params[:id]

  sort = (params[:sort] || 'description').downcase
  sort = 'groups.description' if sort == 'description'
  dir = (params[:dir] || 'asc').downcase
  query_filter = params[:query_filter].strip rescue nil

  statement = id.blank? ? Group : type.constantize.find(id).groups
  statement = (params[:query_filter].blank? ? statement : ar.where("UPPER(groups.description) LIKE UPPER('%#{query_filter}%')"))
  selected = statement.paginate(:page => page, :per_page => per_page, :order => "#{sort} #{dir}")

  render :json => {:total_count => statement.count, :groups => selected.map { |group| group.to_data_hash }}
end

#updateObject



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
# File 'app/controllers/api/v1/groups_controller.rb', line 174

def update
  begin
    ActiveRecord::Base.connection.transaction do
      group = Group.find(params[:id])

      group.description = params[:description].strip
      group.save!

      render json: {success: true, group: group.to_data_hash}
    end
  rescue ActiveRecord::RecordInvalid => invalid
    Rails.logger.error invalid.record.errors

    message = "<ul>"
    invalid.record.errors.collect do |e, m|
      message << "<li>#{e} #{m}</li>"
    end
    message << "</ul>"

    render :json => {:success => false, :message => message}
  rescue StandardError => ex
    Rails.logger.error ex.message
    Rails.logger.error ex.backtrace.join("\n")

    ExceptionNotifier.notify_exception(ex) if defined? ExceptionNotifier

    render :json => {:success => false, :message => 'Error update Group'}
  end
end