anoubis_sso_client 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/README.md +41 -0
- data/Rakefile +15 -0
- data/app/controllers/anoubis_sso_client/application_common.rb +516 -0
- data/app/controllers/anoubis_sso_client/application_controller.rb +5 -0
- data/app/controllers/anoubis_sso_client/data_controller.rb +5 -0
- data/app/controllers/anoubis_sso_client/index_controller.rb +30 -0
- data/app/models/anoubis_sso_client/application_record.rb +125 -0
- data/app/models/anoubis_sso_client/group.rb +101 -0
- data/app/models/anoubis_sso_client/group_menu.rb +107 -0
- data/app/models/anoubis_sso_client/group_user.rb +25 -0
- data/app/models/anoubis_sso_client/menu.rb +220 -0
- data/app/models/anoubis_sso_client/user.rb +91 -0
- data/config/locales/en.yml +28 -0
- data/config/locales/ru.yml +28 -0
- data/config/routes.rb +23 -0
- data/db/migrate/20220302061651_create_anoubis_sso_client_users.rb +15 -0
- data/db/migrate/20220315061539_create_anoubis_sso_client_groups.rb +11 -0
- data/db/migrate/20220413110404_create_anoubis_sso_client_menus.rb +21 -0
- data/db/migrate/20220415131249_create_anoubis_sso_client_group_menus.rb +12 -0
- data/db/migrate/20220420054028_create_anoubis_sso_client_group_users.rb +10 -0
- data/db/seeds.rb +15 -0
- data/lib/anoubis_sso_client/engine.rb +13 -0
- data/lib/anoubis_sso_client/version.rb +4 -0
- data/lib/anoubis_sso_client.rb +7 -0
- metadata +240 -0
@@ -0,0 +1,125 @@
|
|
1
|
+
##
|
2
|
+
# Main Active Record object inherited from {https://www.rubydoc.info/gems/anoubis/Anoubis/ApplicationRecord Anoubis::ApplicationRecord}
|
3
|
+
class AnoubisSsoClient::ApplicationRecord < Anoubis::ApplicationRecord
|
4
|
+
self.abstract_class = true
|
5
|
+
|
6
|
+
##
|
7
|
+
# Returns SSO Menu model.
|
8
|
+
# Can be redefined in Rails.application configuration_anoubis_sso_menu_model configuration parameter.
|
9
|
+
# By default returns {AnoubisSsoClient::Menu} model class
|
10
|
+
# @return [Class] Menu model class
|
11
|
+
def self.menu_model
|
12
|
+
begin
|
13
|
+
value = Object.const_get Rails.configuration.anoubis_sso_menu_model
|
14
|
+
rescue StandardError
|
15
|
+
value = AnoubisSsoClient::Menu
|
16
|
+
end
|
17
|
+
|
18
|
+
value
|
19
|
+
end
|
20
|
+
|
21
|
+
##
|
22
|
+
# Returns SSO Menu model.
|
23
|
+
# Can be redefined in Rails.application configuration_anoubis_sso_menu_model configuration parameter.
|
24
|
+
# By default returns {AnoubisSsoClient::Menu} model class
|
25
|
+
# @return [Class] Menu model class
|
26
|
+
def menu_model
|
27
|
+
AnoubisSsoClient::ApplicationRecord.menu_model
|
28
|
+
end
|
29
|
+
|
30
|
+
##
|
31
|
+
# Returns SSO Group model.
|
32
|
+
# Can be redefined in Rails.application configuration_anoubis_sso_group_model configuration parameter.
|
33
|
+
# By default returns {AnoubisSsoClient::Group} model class
|
34
|
+
# @return [Class] Group model class
|
35
|
+
def self.group_model
|
36
|
+
begin
|
37
|
+
value = Object.const_get Rails.configuration.anoubis_sso_group_model
|
38
|
+
rescue StandardError
|
39
|
+
value = AnoubisSsoClient::Group
|
40
|
+
end
|
41
|
+
|
42
|
+
value
|
43
|
+
end
|
44
|
+
|
45
|
+
##
|
46
|
+
# Returns SSO Group model.
|
47
|
+
# Can be redefined in Rails.application configuration_anoubis_sso_group_model configuration parameter.
|
48
|
+
# By default returns {AnoubisSsoClient::Group} model class
|
49
|
+
# @return [Class] Group model class
|
50
|
+
def group_model
|
51
|
+
AnoubisSsoClient::ApplicationRecord.group_model
|
52
|
+
end
|
53
|
+
|
54
|
+
##
|
55
|
+
# Returns SSO GroupMenu model.
|
56
|
+
# Can be redefined in Rails.application configuration_anoubis_sso_group_menu_model configuration parameter.
|
57
|
+
# By default returns {AnoubisSsoClient::GroupMenu} model class
|
58
|
+
# @return [Class] GroupMenu model class
|
59
|
+
def self.group_menu_model
|
60
|
+
begin
|
61
|
+
value = Object.const_get Rails.configuration.anoubis_sso_group_menu_model
|
62
|
+
rescue StandardError
|
63
|
+
value = AnoubisSsoClient::GroupMenu
|
64
|
+
end
|
65
|
+
|
66
|
+
value
|
67
|
+
end
|
68
|
+
|
69
|
+
##
|
70
|
+
# Returns SSO GroupMenu model.
|
71
|
+
# Can be redefined in Rails.application configuration_anoubis_sso_group_menu_model configuration parameter.
|
72
|
+
# By default returns {AnoubisSsoClient::GroupMenu} model class
|
73
|
+
# @return [Class] GroupMenu model class
|
74
|
+
def group_menu_model
|
75
|
+
AnoubisSsoClient::ApplicationRecord.group_menu_model
|
76
|
+
end
|
77
|
+
|
78
|
+
##
|
79
|
+
# Returns SSO GroupUser model.
|
80
|
+
# Can be redefined in Rails.application configuration_anoubis_sso_group_user_model configuration parameter.
|
81
|
+
# By default returns {AnoubisSsoClient::GroupUser} model class
|
82
|
+
# @return [Class] GroupUser model class
|
83
|
+
def self.group_user_model
|
84
|
+
begin
|
85
|
+
value = Object.const_get Rails.configuration.anoubis_sso_group_user_model
|
86
|
+
rescue StandardError
|
87
|
+
value = AnoubisSsoClient::GroupUser
|
88
|
+
end
|
89
|
+
|
90
|
+
value
|
91
|
+
end
|
92
|
+
|
93
|
+
##
|
94
|
+
# Returns SSO GroupUser model.
|
95
|
+
# Can be redefined in Rails.application configuration_anoubis_sso_group_user_model configuration parameter.
|
96
|
+
# By default returns {AnoubisSsoClient::GroupUser} model class
|
97
|
+
# @return [Class] GroupUser model class
|
98
|
+
def group_user_model
|
99
|
+
AnoubisSsoClient::ApplicationRecord.group_user_model
|
100
|
+
end
|
101
|
+
|
102
|
+
##
|
103
|
+
# Returns SSO User model.
|
104
|
+
# Can be redefined in Rails.application configuration_anoubis_sso_user_model configuration parameter.
|
105
|
+
# By default returns {AnoubisSsoClient::User} model class
|
106
|
+
# @return [Class] User model class
|
107
|
+
def self.user_model
|
108
|
+
begin
|
109
|
+
value = Object.const_get Rails.configuration.anoubis_sso_user_model
|
110
|
+
rescue
|
111
|
+
value = AnoubisSsoClient::User
|
112
|
+
end
|
113
|
+
|
114
|
+
value
|
115
|
+
end
|
116
|
+
|
117
|
+
##
|
118
|
+
# Returns SSO User model.
|
119
|
+
# Can be redefined in Rails.application configuration_anoubis_sso_user_model configuration parameter.
|
120
|
+
# By default returns {AnoubisSsoClient::User} model class
|
121
|
+
# @return [Class] User model class
|
122
|
+
def user_model
|
123
|
+
AnoubisSsoClient::ApplicationRecord.user_model
|
124
|
+
end
|
125
|
+
end
|
@@ -0,0 +1,101 @@
|
|
1
|
+
##
|
2
|
+
# User group model
|
3
|
+
class AnoubisSsoClient::Group < AnoubisSsoClient::ApplicationRecord
|
4
|
+
self.table_name = 'groups'
|
5
|
+
|
6
|
+
## Identifier validation constant
|
7
|
+
VALID_IDENT_REGEX = /\A[a-z]*\z/i
|
8
|
+
|
9
|
+
# @!attribute ident
|
10
|
+
# @return [String] the group's identifier. Identifier consists of lowercase alphabetical symbols.
|
11
|
+
validates :ident, length: { minimum: 3, maximum: 50 }, uniqueness: { case_sensitive: false }, format: { with: VALID_IDENT_REGEX }
|
12
|
+
|
13
|
+
validates :title, presence: true, length: { maximum: 100 }
|
14
|
+
|
15
|
+
has_many :group_menus, class_name: 'AnoubisSsoClient::GroupMenu'
|
16
|
+
has_many :group_users, class_name: 'AnoubisSsoClient::GroupUser'
|
17
|
+
|
18
|
+
##
|
19
|
+
# Returns title according by I18n.locale
|
20
|
+
# @return [String] Localized title
|
21
|
+
def title
|
22
|
+
get_locale_field 'title_locale'
|
23
|
+
end
|
24
|
+
|
25
|
+
##
|
26
|
+
# Defines title according by I18n.locale
|
27
|
+
# @param value [String] Localized title
|
28
|
+
def title=(value)
|
29
|
+
set_locale_field 'title_locale', value
|
30
|
+
end
|
31
|
+
|
32
|
+
##
|
33
|
+
# Create multi language group
|
34
|
+
# @param [Hash] params initial model options
|
35
|
+
# @option params [String] :ident group identifier
|
36
|
+
# @option params [String] :translate translate identifier
|
37
|
+
# @return [Group] returns created group object
|
38
|
+
def self.create_group(params)
|
39
|
+
return nil if !params.key? :ident
|
40
|
+
return nil if !params.key? :translate
|
41
|
+
|
42
|
+
group = group_model.find_or_create_by ident: params[:ident]
|
43
|
+
|
44
|
+
return nil unless group
|
45
|
+
|
46
|
+
I18n.available_locales.each do |locale|
|
47
|
+
I18n.locale = locale
|
48
|
+
group.title = I18n.t(params[:translate])
|
49
|
+
end
|
50
|
+
group.save if group.changed?
|
51
|
+
|
52
|
+
group
|
53
|
+
end
|
54
|
+
|
55
|
+
##
|
56
|
+
# Add access to menu element for current group
|
57
|
+
# @param [Hash] params parameters
|
58
|
+
# @option params [Menu | Array<Menu>] :menu {Menu} model or array of {Menu} models
|
59
|
+
# @option params [String] :access access mode ('read', 'write', 'disable'). Optional. By default set to 'read'
|
60
|
+
def add_menu(params = {})
|
61
|
+
return if !params.has_key? :menu
|
62
|
+
|
63
|
+
params[:access] = 'read' unless params.key? :access
|
64
|
+
|
65
|
+
if params[:menu].class == Array
|
66
|
+
params[:menu].each do |menu|
|
67
|
+
data = group_menu_model.find_or_create_by group_id: id, menu: menu
|
68
|
+
if group_menu_model.accesses[params[:access].to_sym] > group_menu_model.accesses[data.access.to_sym]
|
69
|
+
data.access = params[:access]
|
70
|
+
data.save
|
71
|
+
end
|
72
|
+
end
|
73
|
+
else
|
74
|
+
data = group_menu_model.find_or_create_by group_id: id, menu: params[:menu]
|
75
|
+
if group_menu_model.accesses[params[:access].to_sym] > group_menu_model.accesses[data.access.to_sym]
|
76
|
+
data.access = params[:access]
|
77
|
+
data.save
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
nil
|
82
|
+
end
|
83
|
+
|
84
|
+
##
|
85
|
+
# Add user to current group
|
86
|
+
# @param [Hash] params parameters
|
87
|
+
# @option params [User | Array<User>] :user {User} model or array of {User} models
|
88
|
+
def add_user(params = {})
|
89
|
+
return if !params.has_key? :user
|
90
|
+
|
91
|
+
if params[:user].class == Array
|
92
|
+
params[:user].each do |user|
|
93
|
+
group_menu_model.find_or_create_by group_id: id, user: user
|
94
|
+
end
|
95
|
+
else
|
96
|
+
group_menu_model.find_or_create_by group_id: id, user: params[:user]
|
97
|
+
end
|
98
|
+
|
99
|
+
nil
|
100
|
+
end
|
101
|
+
end
|
@@ -0,0 +1,107 @@
|
|
1
|
+
##
|
2
|
+
# Model links {Menu} and {Group}. Describes group access to menu.
|
3
|
+
class AnoubisSsoClient::GroupMenu < AnoubisSsoClient::ApplicationRecord
|
4
|
+
# Redefines default table name
|
5
|
+
self.table_name = 'group_menus'
|
6
|
+
|
7
|
+
before_validation :before_validation_sso_client_group_menu
|
8
|
+
after_create :after_create_sso_client_group_menu
|
9
|
+
before_update :before_update_sso_client_group_menu
|
10
|
+
after_destroy :after_destroy_sso_client_group_menu
|
11
|
+
|
12
|
+
# @!attribute group
|
13
|
+
# @return [Group] reference to the {Group} model
|
14
|
+
belongs_to :group, class_name: 'AnoubisSsoClient::Group'
|
15
|
+
validates :group, presence: true, uniqueness: { scope: [:menu_id] }
|
16
|
+
|
17
|
+
# @!attribute menu
|
18
|
+
# @return [Menu] reference to the {Menu} model
|
19
|
+
belongs_to :menu, class_name: 'AnoubisSsoClient::Menu'
|
20
|
+
validates :menu, presence: true, uniqueness: { scope: [:group_id] }
|
21
|
+
|
22
|
+
# @!attribute access
|
23
|
+
# @return ['read', 'write', 'disable'] group access to menu element.
|
24
|
+
# - 'read' --- group has access to menu element only for read data
|
25
|
+
# - 'write' --- group has access to menu element for read and write data
|
26
|
+
# - 'disable' --- group hasn't access to menu element
|
27
|
+
enum access: { read: 0, write: 50, disable: 100 }
|
28
|
+
|
29
|
+
##
|
30
|
+
# Is called before validation when the link between menu and group is being created or updated.
|
31
|
+
# Procedure checks if group belongs a system that has access to this menu element. If {#access} doesn't
|
32
|
+
# defined then {#access} sets to 'read'
|
33
|
+
def before_validation_sso_client_group_menu
|
34
|
+
self.access = 'read' unless access
|
35
|
+
end
|
36
|
+
|
37
|
+
##
|
38
|
+
# Is called after new link between menu and group was created. If new element has parent with link that
|
39
|
+
# doesn't present in database then adds this link to database with {#access} defined as 'read'.
|
40
|
+
def after_create_sso_client_group_menu
|
41
|
+
if menu.menu_id
|
42
|
+
AnoubisSsoClient::GroupMenu.find_or_create_by(menu_id: menu.menu_id, group_id: group_id) do |menu|
|
43
|
+
menu.access = 'read'
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
##
|
49
|
+
# Is called before link between menu and group will be updated. Procedure prevents changing {#menu}
|
50
|
+
# and {#group} value.
|
51
|
+
def before_update_sso_client_group_menu
|
52
|
+
self.menu_id = menu_id_was if menu_id_changed?
|
53
|
+
self.group_id = group_id_was if group_id_changed?
|
54
|
+
end
|
55
|
+
|
56
|
+
##
|
57
|
+
# Is called after link between menu and group had been deleted from database. It also deletes all child links.
|
58
|
+
def after_destroy_sso_client_group_menu
|
59
|
+
AnoubisSsoClient::Menu.select(:id).where(menu_id: menu_id).each do |menu|
|
60
|
+
AnoubisSsoClient::GroupMenu.where(menu_id: menu.id, group_id: group_id).each do |group_menu|
|
61
|
+
group_menu.destroy
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
##
|
67
|
+
# Return localized title of menu element
|
68
|
+
# @return [String] localized title
|
69
|
+
def title
|
70
|
+
get_localized_menu 'title'
|
71
|
+
end
|
72
|
+
|
73
|
+
##
|
74
|
+
# Return localized page_title of menu element
|
75
|
+
# @return [String] localized page_title
|
76
|
+
def page_title
|
77
|
+
get_localized_menu 'page_title'
|
78
|
+
end
|
79
|
+
|
80
|
+
##
|
81
|
+
# Return localized short_title of menu element
|
82
|
+
# @return [String] localized short_title
|
83
|
+
def short_title
|
84
|
+
get_localized_menu 'short_title'
|
85
|
+
end
|
86
|
+
|
87
|
+
##
|
88
|
+
# Return localized field
|
89
|
+
# @param [String] field Field name
|
90
|
+
# @return [String] localized field
|
91
|
+
def get_localized_menu(field)
|
92
|
+
loc_field = (field.to_s + '_locale').to_sym
|
93
|
+
begin
|
94
|
+
self[loc_field] = JSON.parse(self[loc_field]) if self[loc_field]
|
95
|
+
rescue
|
96
|
+
|
97
|
+
end
|
98
|
+
|
99
|
+
begin
|
100
|
+
result = get_locale_field loc_field.to_s
|
101
|
+
rescue
|
102
|
+
result = eval('self.menu.' + field.to_s)
|
103
|
+
end
|
104
|
+
|
105
|
+
result
|
106
|
+
end
|
107
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
##
|
2
|
+
# Model links {Menu} and {User}. Describes users that belongs to group.
|
3
|
+
class AnoubisSsoClient::GroupUser < AnoubisSsoClient::ApplicationRecord
|
4
|
+
# Redefines default table name
|
5
|
+
self.table_name = 'group_users'
|
6
|
+
|
7
|
+
before_update :before_update_sso_client_group_user
|
8
|
+
|
9
|
+
# @!attribute group
|
10
|
+
# @return [Group] reference to the {Group} model
|
11
|
+
belongs_to :group, class_name: 'AnoubisSsoClient::Group'
|
12
|
+
validates :group, presence: true, uniqueness: { scope: [:user_id] }
|
13
|
+
|
14
|
+
# @!attribute user
|
15
|
+
# @return [User] reference to the {User} model
|
16
|
+
belongs_to :user, class_name: 'AnoubisSsoClient::User'
|
17
|
+
validates :user, presence: true, uniqueness: { scope: [:group_id] }
|
18
|
+
|
19
|
+
##
|
20
|
+
# Procedure prevents changing {User} and {Group} data.
|
21
|
+
def before_update_sso_client_group_user
|
22
|
+
self.user_id = user_id_was if user_id.changed?
|
23
|
+
self.group_id = group_id_was if group_id.changed?
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,220 @@
|
|
1
|
+
##
|
2
|
+
# Main menu model
|
3
|
+
class AnoubisSsoClient::Menu < AnoubisSsoClient::ApplicationRecord
|
4
|
+
# Redefines default table name
|
5
|
+
self.table_name = 'menus'
|
6
|
+
|
7
|
+
before_create :before_sso_client_create_menu
|
8
|
+
before_update :before_sso_client_update_menu
|
9
|
+
before_destroy :before_sso_client_destroy_menu
|
10
|
+
after_destroy :after_sso_client_destroy_menu
|
11
|
+
|
12
|
+
# @!attribute mode
|
13
|
+
# @return [String] the controller path for menu element.
|
14
|
+
validates :mode, presence: true, uniqueness: true
|
15
|
+
|
16
|
+
# @!attribute action
|
17
|
+
# @return [String] the default action of menu element ('data', 'menu', etc.).
|
18
|
+
validates :action, presence: true
|
19
|
+
|
20
|
+
# @!attribute tab
|
21
|
+
# @return [Integer] the nesting level of menu element
|
22
|
+
validates :tab, presence: true, numericality: { only_integer: true, greater_than_or_equal_to: 0 }
|
23
|
+
|
24
|
+
# @!attribute position
|
25
|
+
# @return [Integer] the order position of menu element in current level.
|
26
|
+
validates :position, presence: true, numericality: { only_integer: true, greater_than_or_equal_to: 0 }
|
27
|
+
|
28
|
+
# @!attribute page_size
|
29
|
+
# @return [Integer] the default page size for table of data frame.
|
30
|
+
validates :page_size, numericality: { only_integer: true, greater_than_or_equal_to: 0 }
|
31
|
+
|
32
|
+
# @!attribute status
|
33
|
+
# @return ['enabled', 'disabled'] the status of menu element.
|
34
|
+
# - 'enabled' --- element is enabled and is used by the system.
|
35
|
+
# - 'disabled' --- element is disabled and isn't used by the system.
|
36
|
+
enum status: { enabled: 0, disabled: 1 }
|
37
|
+
|
38
|
+
# @!attribute state
|
39
|
+
# @return ['visible', 'hidden'] the visibility of menu element. Attribute is used in fronted application.
|
40
|
+
# - 'visible' --- element is visible.
|
41
|
+
# - 'hidden' --- element is hidden.
|
42
|
+
enum state: { visible: 0, hidden: 1 }
|
43
|
+
|
44
|
+
validates :title, presence: true, length: { maximum: 100 }
|
45
|
+
validates :page_title, presence: true, length: { maximum: 200 }
|
46
|
+
validates :short_title, presence: true, length: { maximum: 200 }
|
47
|
+
|
48
|
+
# @!attribute menu
|
49
|
+
# @return [Menu, nil] the parent menu for element menu (if exists).
|
50
|
+
belongs_to :menu, class_name: 'AnoubisSsoClient::Menu', optional: true
|
51
|
+
has_many :menus, class_name: 'AnoubisSsoClient::Menu'
|
52
|
+
has_many :group_menus, class_name: 'AnoubisSsoClient::GroupMenu'
|
53
|
+
|
54
|
+
##
|
55
|
+
# Is called before menu will be created in database. Sets {#position} as last {#position} + 1 on current {#tab}.
|
56
|
+
def before_sso_client_create_menu
|
57
|
+
data = AnoubisSsoClient::Menu.where(menu_id: menu_id).maximum(:position)
|
58
|
+
self.position = data ? data + 1 : 0
|
59
|
+
|
60
|
+
before_sso_client_update_menu
|
61
|
+
end
|
62
|
+
|
63
|
+
##
|
64
|
+
# Is called before menu will be stored in database. Sets {#mode} and {#action} in lowercase. If {#page_size}
|
65
|
+
# doesn't defined then sets it to 20. If defined parent menu element then sets {#tab} based on {#tab} of
|
66
|
+
# parent menu element + 1.
|
67
|
+
def before_sso_client_update_menu
|
68
|
+
self.menu_id = nil if menu_id == ''
|
69
|
+
self.mode = mode.downcase
|
70
|
+
self.action = action.downcase
|
71
|
+
self.page_size = 20 unless page_size
|
72
|
+
self.page_size = page_size.to_i
|
73
|
+
|
74
|
+
parent_menu = AnoubisSsoClient::Menu.where(id: menu_id).first
|
75
|
+
self.tab = parent_menu ? parent_menu.tab + 1 : 0
|
76
|
+
end
|
77
|
+
|
78
|
+
##
|
79
|
+
# Is called before menu will be deleted from database. Procedure clears most child components.
|
80
|
+
def before_sso_client_destroy_menu
|
81
|
+
unless menus.empty?
|
82
|
+
errors.add(:base, I18n.t('activerecord.errors.anoubis_sso_client/menu.errors.has_child_menus'))
|
83
|
+
throw(:abort, __method__)
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
##
|
88
|
+
# Is called after menu was deleted from database. Procedure recalculates position of other menu elements.
|
89
|
+
def after_sso_client_destroy_menu
|
90
|
+
query = <<-SQL
|
91
|
+
UPDATE menus
|
92
|
+
SET menus.position = menus.position - 1
|
93
|
+
WHERE menus.tab = #{tab} AND menus.position > #{position}
|
94
|
+
SQL
|
95
|
+
AnoubisSsoClient::Menu.connection.execute query
|
96
|
+
end
|
97
|
+
|
98
|
+
##
|
99
|
+
# Returns title according by I18n.locale
|
100
|
+
# @return [String] Localized title
|
101
|
+
def title
|
102
|
+
get_locale_field 'title_locale'
|
103
|
+
end
|
104
|
+
|
105
|
+
##
|
106
|
+
# Defines title according by I18n.locale
|
107
|
+
# @param value [String] Localized title
|
108
|
+
def title=(value)
|
109
|
+
set_locale_field 'title_locale', value
|
110
|
+
end
|
111
|
+
|
112
|
+
##
|
113
|
+
# Returns short title according by I18n.locale
|
114
|
+
# @return [String] Localized title
|
115
|
+
def short_title
|
116
|
+
get_locale_field 'short_title_locale'
|
117
|
+
end
|
118
|
+
|
119
|
+
##
|
120
|
+
# Defines short title according by I18n.locale
|
121
|
+
# @param value [String] Localized short title
|
122
|
+
def short_title=(value)
|
123
|
+
set_locale_field 'short_title_locale', value
|
124
|
+
end
|
125
|
+
|
126
|
+
##
|
127
|
+
# Returns page title according by I18n.locale
|
128
|
+
# @return [String] Localized page title
|
129
|
+
def page_title
|
130
|
+
get_locale_field 'page_title_locale'
|
131
|
+
end
|
132
|
+
|
133
|
+
##
|
134
|
+
# Defines page title according by I18n.locale
|
135
|
+
# @param value [String] Localized page title
|
136
|
+
def page_title=(value)
|
137
|
+
set_locale_field 'page_title_locale', value
|
138
|
+
end
|
139
|
+
|
140
|
+
##
|
141
|
+
# Create multi language menu
|
142
|
+
# @param [Hash] params initial model options
|
143
|
+
# @option params [String] :mode menu identifier
|
144
|
+
# @option params [String] :action menu action type ('data', 'menu' and etc.)
|
145
|
+
# @option params [String] :access access mode ('read', 'write', 'disable'). Optional. By default set to 'read'
|
146
|
+
# @option params [String] :state menu visibility ('visible', 'hidden'). Optional. By default set to 'visible'
|
147
|
+
# @option params [String] :page_size default table size for action 'data'. Optional. By default set 20
|
148
|
+
# @option params [String] :group User's group or array of user's group. Optional.
|
149
|
+
# @return [AnoubisSsoClient::Menu] returns created menu object
|
150
|
+
def self.create_menu(params)
|
151
|
+
return nil if !params.key? :mode
|
152
|
+
return nil if !params.key? :action
|
153
|
+
|
154
|
+
params[:access] = 'read' unless params.key? :access
|
155
|
+
params[:state] = 'visible' unless params.key? :state
|
156
|
+
|
157
|
+
data = AnoubisSsoClient::Menu.where(mode: params[:mode]).first
|
158
|
+
data = AnoubisSsoClient::Menu.create({ mode: params[:mode], action: params[:action] }) unless data
|
159
|
+
|
160
|
+
return unless data
|
161
|
+
|
162
|
+
data.action = params[:action]
|
163
|
+
if params.key? :parent
|
164
|
+
data.menu = params[:parent]
|
165
|
+
else
|
166
|
+
data.menu_id = nil
|
167
|
+
end
|
168
|
+
data.page_size = params.key?(:page_size) ? params[:page_size] : 20
|
169
|
+
data.state = params[:state]
|
170
|
+
|
171
|
+
prefix = "install.menu.#{params[:mode]}"
|
172
|
+
|
173
|
+
I18n.available_locales.each do |locale|
|
174
|
+
I18n.locale = locale
|
175
|
+
data.title = I18n.t("#{prefix}.title")
|
176
|
+
data.page_title = I18n.t("#{prefix}.page_title")
|
177
|
+
data.short_title = I18n.t("#{prefix}.short_title", default: data.title)
|
178
|
+
end
|
179
|
+
|
180
|
+
if data.changed?
|
181
|
+
unless data.save
|
182
|
+
puts data.errors.full_messages
|
183
|
+
return nil
|
184
|
+
end
|
185
|
+
end
|
186
|
+
|
187
|
+
data.add_group params
|
188
|
+
|
189
|
+
data
|
190
|
+
end
|
191
|
+
|
192
|
+
##
|
193
|
+
# Add access to group for menu element
|
194
|
+
# @param [Hash] params parameters
|
195
|
+
# @option params [Group | Array<Group>] :group <Group> model or array of <Group> models
|
196
|
+
# @option params [String] :access access mode ('read', 'write', 'disable'). Optional. By default set to 'read'
|
197
|
+
def add_group(params = {})
|
198
|
+
return if !params.has_key? :group
|
199
|
+
|
200
|
+
params[:access] = 'read' unless params.key? :access
|
201
|
+
|
202
|
+
if params[:group].class == Array
|
203
|
+
params[:group].each do |group|
|
204
|
+
data = group_menu_model.find_or_create_by group: group, menu_id: id
|
205
|
+
if group_menu_model.accesses[params[:access].to_sym] > group_menu_model.accesses[data.access.to_sym]
|
206
|
+
data.access = params[:access]
|
207
|
+
data.save
|
208
|
+
end
|
209
|
+
end
|
210
|
+
else
|
211
|
+
data = group_menu_model.find_or_create_by group: params[:group], menu_id: id
|
212
|
+
if group_menu_model.accesses[params[:access].to_sym] > group_menu_model.accesses[data.access.to_sym]
|
213
|
+
data.access = params[:access]
|
214
|
+
data.save
|
215
|
+
end
|
216
|
+
end
|
217
|
+
|
218
|
+
nil
|
219
|
+
end
|
220
|
+
end
|
@@ -0,0 +1,91 @@
|
|
1
|
+
##
|
2
|
+
# User model
|
3
|
+
class AnoubisSsoClient::User < AnoubisSsoClient::ApplicationRecord
|
4
|
+
self.table_name = 'users'
|
5
|
+
|
6
|
+
after_create :after_sso_client_create_user
|
7
|
+
|
8
|
+
## Timezone
|
9
|
+
attr_accessor :timezone
|
10
|
+
|
11
|
+
## Locale
|
12
|
+
attr_accessor :locale
|
13
|
+
|
14
|
+
before_validation :before_validation_sso_client_user_on_create, on: :create
|
15
|
+
|
16
|
+
validates :sso_uuid, presence: true, uniqueness: { case_sensitive: true }
|
17
|
+
validates :uuid, presence: true, length: { maximum: 40 }, uniqueness: { case_sensitive: true }
|
18
|
+
|
19
|
+
has_many :group_users, class_name: 'AnoubisSsoClient::GroupUser'
|
20
|
+
|
21
|
+
##
|
22
|
+
# Fires before create any User on the server. Procedure generates internal UUID and setup timezone to GMT.
|
23
|
+
# Public user identifier is generated also if not defined.
|
24
|
+
def before_validation_sso_client_user_on_create
|
25
|
+
self.uuid = setup_private_user_id
|
26
|
+
end
|
27
|
+
|
28
|
+
##
|
29
|
+
# Procedure setup private user identifier. Procedure can be redefined.
|
30
|
+
# @return [String] public user identifier
|
31
|
+
def setup_private_user_id
|
32
|
+
SecureRandom.uuid
|
33
|
+
end
|
34
|
+
|
35
|
+
##
|
36
|
+
# Update user data information in user database
|
37
|
+
# @param user_data [Hash] - User data information received from SSO server
|
38
|
+
def update_user_data(user_data)
|
39
|
+
self.name = user_data[:name] if user_data.key? :name
|
40
|
+
self.surname = user_data[:surname] if user_data.key? :surname
|
41
|
+
self.email = user_data[:email] if user_data.key? :email
|
42
|
+
self.timezone = user_data.key?(:timezone) ? user_data[:timezone] : 'GMT'
|
43
|
+
self.locale = user_data.key?(:locale) ? user_data[:locale] : 'ru-RU'
|
44
|
+
end
|
45
|
+
|
46
|
+
##
|
47
|
+
# Returns user data for saving into the session
|
48
|
+
def session_data
|
49
|
+
{
|
50
|
+
name: name,
|
51
|
+
surname: surname,
|
52
|
+
email: email,
|
53
|
+
locale: locale,
|
54
|
+
timezone: timezone
|
55
|
+
}
|
56
|
+
end
|
57
|
+
|
58
|
+
##
|
59
|
+
# Fires after create new user.
|
60
|
+
def after_sso_client_create_user
|
61
|
+
attach_groups
|
62
|
+
end
|
63
|
+
|
64
|
+
##
|
65
|
+
# Procedure attaches groups to new created user. By default visitor group is attached to new user
|
66
|
+
def attach_groups
|
67
|
+
gr = group_model.where(ident: 'visitor').first
|
68
|
+
|
69
|
+
return unless gr
|
70
|
+
|
71
|
+
add_group group: gr
|
72
|
+
end
|
73
|
+
|
74
|
+
##
|
75
|
+
# Add current user to the group
|
76
|
+
# @param [Hash] params parameters
|
77
|
+
# @option params [Group | Array<Group>] :group <Group> model or array of <Group> models
|
78
|
+
def add_group(params = {})
|
79
|
+
return if !params.has_key? :group
|
80
|
+
|
81
|
+
if params[:group].class == Array
|
82
|
+
params[:group].each do |group|
|
83
|
+
group_user_model.find_or_create_by group: group, user_id: id
|
84
|
+
end
|
85
|
+
else
|
86
|
+
group_user_model.find_or_create_by group: params[:group], user_id: id
|
87
|
+
end
|
88
|
+
|
89
|
+
nil
|
90
|
+
end
|
91
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
en:
|
2
|
+
anoubis:
|
3
|
+
install:
|
4
|
+
groups:
|
5
|
+
admin: "Administrators"
|
6
|
+
user: "Users"
|
7
|
+
visitor: "Visitor"
|
8
|
+
|
9
|
+
activerecord:
|
10
|
+
errors:
|
11
|
+
anoubis_sso_client/menu:
|
12
|
+
errors:
|
13
|
+
has_child_menus: "Has child menu elements"
|
14
|
+
|
15
|
+
install:
|
16
|
+
menu:
|
17
|
+
dashboard:
|
18
|
+
title: "Dashboard"
|
19
|
+
page_title: "Dashboard"
|
20
|
+
short_title: "Dashboard"
|
21
|
+
settings:
|
22
|
+
title: "Settings"
|
23
|
+
page_title: "Settings"
|
24
|
+
short_title: "Settings"
|
25
|
+
settings/menu:
|
26
|
+
title: "Menu"
|
27
|
+
page_title: "Menu administration"
|
28
|
+
short_title: "Menu"
|