Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
A
Admin
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
osuny
Admin
Commits
8d4ad703
Commit
8d4ad703
authored
3 years ago
by
pabois
Browse files
Options
Downloads
Patches
Plain Diff
roles
parent
853ebb85
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
app/models/user.rb
+1
-2
1 addition, 2 deletions
app/models/user.rb
app/models/user/with_authentication.rb
+15
-2
15 additions, 2 deletions
app/models/user/with_authentication.rb
app/models/user/with_roles.rb
+27
-0
27 additions, 0 deletions
app/models/user/with_roles.rb
with
43 additions
and
4 deletions
app/models/user.rb
+
1
−
2
View file @
8d4ad703
...
...
@@ -54,8 +54,7 @@
#
class
User
<
ApplicationRecord
include
WithAuthentication
enum
role:
{
visitor:
0
,
admin:
20
,
superadmin:
30
}
include
WithRoles
belongs_to
:university
belongs_to
:language
...
...
This diff is collapsed.
Click to expand it.
app/models/user/with_authentication.rb
+
15
−
2
View file @
8d4ad703
...
...
@@ -3,15 +3,24 @@ module User::WithAuthentication
included
do
devise
:database_authenticatable
,
:registerable
,
:recoverable
,
:rememberable
,
:timeoutable
,
:validatable
,
:confirmable
,
:trackable
,
:lockable
,
:two_factor_authenticatable
:timeoutable
,
:confirmable
,
:trackable
,
:lockable
,
:two_factor_authenticatable
# note : i do not use :validatable because of the non-uniqueness of the email. :validatable is replaced by the validation sequences below
has_one_time_password
(
encrypted:
true
)
validates_presence_of
:first_name
,
:last_name
,
:email
validates
:role
,
presence:
true
validates_presence_of
:first_name
,
:last_name
,
:email
validates_uniqueness_of
:email
,
scope: :university_id
,
allow_blank:
true
,
if: :will_save_change_to_email?
validates_format_of
:email
,
with:
Devise
::
email_regexp
,
allow_blank:
true
,
if: :will_save_change_to_email?
validates_presence_of
:password
,
if: :password_required?
validates_confirmation_of
:password
,
if: :password_required?
validate
:password_complexity
validates
:mobile_phone
,
format:
{
with:
/\A\+[0-9]+\z/
},
allow_blank:
true
before_validation
:adjust_mobile_phone
,
:sanitize_fields
def
self
.
find_for_authentication
(
warden_conditions
)
...
...
@@ -64,6 +73,10 @@ module User::WithAuthentication
self
.
mobile_phone
=
full_sanitizer
.
sanitize
(
self
.
mobile_phone
)
&
.
gsub
(
'='
,
''
)
end
def
password_required?
!
persisted?
||
!
password
.
nil?
||
!
password_confirmation
.
nil?
end
def
password_complexity
# Regexp extracted from https://stackoverflow.com/questions/19605150/regex-for-password-must-contain-at-least-eight-characters-at-least-one-number-a
return
if
password
.
blank?
||
password
=~
/^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[
#{
Rails
.
application
.
config
.
allowed_special_chars
}
]).{
#{
Devise
.
password_length
.
first
}
,
#{
Devise
.
password_length
.
last
}
}$/
...
...
This diff is collapsed.
Click to expand it.
app/models/user/with_roles.rb
0 → 100644
+
27
−
0
View file @
8d4ad703
module
User::WithRoles
extend
ActiveSupport
::
Concern
included
do
attr_accessor
:modified_by
enum
role:
{
visitor:
0
,
admin:
20
,
superadmin:
30
}
scope
:for_role
,
->
(
role
)
{
where
(
role:
role
)
}
before_validation
:check_modifier_role
def
roles_managed
User
.
roles
.
map
do
|
role_name
,
role_id
|
next
if
role_id
>
User
.
roles
[
role
]
role_name
end
.
compact
end
protected
def
check_modifier_role
errors
.
add
(
:role
,
'cannot be set to this role'
)
if
modified_by
&&
!
modified_by
.
roles_managed
.
include?
(
self
.
role
)
end
end
end
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment