-
Notifications
You must be signed in to change notification settings - Fork 38
/
data_access.rb
60 lines (57 loc) · 1.8 KB
/
data_access.rb
1
2
3
4
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
53
54
55
56
57
58
59
60
module DataAccess
# Limit search results according to the current user's data access
# permissions, or, if no user is logged in, limit access to checked
# data of access level 4.
#
# Administrators (that is, users with page_access_level 1) have view
# access to all data, checked or not. Managers can access all of
# this data as well except for data restricted to access_level 1
# users. All other users can see only checked data, and only data
# with a access level number at least as high as the user's access
# level number. Notwithstanding these restrictions, all users have
# access to data marked with their user id.
#
# == Usage
#
# class MyModel < ActiveRecord::Base
# extend DataAccess
# (...)
# end
#
# class ControllerForMyModel < ApplicationController
# def my_search_method(params...)
# @results = MyModel.all_limited(current_user)...
# (...)
# end
# (...)
# end
#
# It is assumed MyModel has a user_id column, an access_level
# column, and a checked column.
#
def all_limited(current_user)
if !current_user.nil?
if current_user.page_access_level == 1
checked = -1
access_level = 1
elsif current_user.page_access_level <= 2
checked = -1
access_level = current_user.access_level
else
checked = 1
access_level = current_user.access_level
end
user = current_user
else
user = 1000000000000
checked = 1
access_level = 4
end
if column_names.include?("checked") and column_names.include?("user_id")
where("(#{table_name}.checked >= ? and #{table_name}.access_level >= ?) or #{table_name}.user_id = ?",
checked, access_level, user)
else
where("#{table_name}.access_level >= ?", access_level)
end
end
end