Class: Decidim::User
- Inherits:
-
UserBaseEntity
- Object
- ActiveRecord::Base
- ApplicationRecord
- UserBaseEntity
- Decidim::User
- Includes:
- ActsAsAuthor, DownloadYourData, Searchable, Traceable, UserReportable
- Defined in:
- decidim-core/app/models/decidim/user.rb
Overview
A User is a participant that wants to join the platform to engage.
Defined Under Namespace
Classes: Roles
Constant Summary collapse
- REGEXP_NICKNAME =
/\A[\w-]+\z/
Constants inherited from UserBaseEntity
Decidim::UserBaseEntity::REGEXP_NAME
Instance Attribute Summary collapse
-
#invitation_instructions ⇒ Object
Public: Allows customizing the invitation instruction email content when inviting a user.
-
#newsletter_notifications ⇒ Object
Returns the value of attribute newsletter_notifications.
Class Method Summary collapse
- .download_your_data_images(user) ⇒ Object
- .export_serializer ⇒ Object
-
.find_for_authentication(warden_conditions) ⇒ Object
Check if the user exists with the given email and the current organization.
-
.has_pending_invitations?(organization_id, email) ⇒ Boolean
Returns the user corresponding to the given
email
if it exists and has pending invitations, otherwise returns nil. - .log_presenter_class_for(_log) ⇒ Object
- .user_collection(user) ⇒ Object
Instance Method Summary collapse
-
#accepted_user_groups ⇒ Object
return the groups where this user has been accepted.
-
#accepts_conversation?(user) ⇒ Boolean
Public: whether the user accepts direct messages from another.
-
#active_role ⇒ Object
Public: Returns the active role of the user.
- #admin_terms_accepted? ⇒ Boolean
- #after_confirmation ⇒ Object
- #authenticatable_salt ⇒ Object
- #being_impersonated? ⇒ Boolean
-
#deleted? ⇒ Boolean
Check if the user account has been deleted or not.
- #follows?(followable) ⇒ Boolean
- #interested_scopes ⇒ Object
- #interested_scopes_ids ⇒ Object
- #invalidate_all_sessions! ⇒ Object
- #invitation_pending? ⇒ Boolean
-
#manageable_user_groups ⇒ Object
return the groups where this user has admin permissions.
- #moderator? ⇒ Boolean
-
#name ⇒ Object
Public: returns the user’s name or the default one.
- #needs_password_update? ⇒ Boolean
- #notifications_subscriptions ⇒ Object
-
#officialized? ⇒ Boolean
Public: whether the user has been officialized or not.
-
#presenter ⇒ Object
Returns the presenter for this author, to be used in the views.
-
#role?(role) ⇒ Boolean
Checks if the user has the given ‘role` or not.
- #tos_accepted? ⇒ Boolean
- #unread_conversations ⇒ Object
- #unread_messages_count ⇒ Object
- #user_invited? ⇒ Boolean
- #user_name ⇒ Object
-
#verifiable? ⇒ Boolean
Whether this user can be verified against some authorization or not.
Methods included from Searchable
searchable_resources, searchable_resources_by_type, searchable_resources_of_type_comment, searchable_resources_of_type_component, searchable_resources_of_type_participant, searchable_resources_of_type_participatory_space
Methods inherited from UserBaseEntity
#followings_blocked?, #public_followings, #public_users_followings, ransackable_associations, ransackable_attributes, #users_followings
Methods included from HasUploadValidations
#attached_uploader, #maximum_avatar_size, #maximum_upload_size
Methods included from Followable
Instance Attribute Details
#invitation_instructions ⇒ Object
Public: Allows customizing the invitation instruction email content when inviting a user.
Returns a String.
101 102 103 |
# File 'decidim-core/app/models/decidim/user.rb', line 101 def invitation_instructions @invitation_instructions end |
#newsletter_notifications ⇒ Object
Returns the value of attribute newsletter_notifications.
78 79 80 |
# File 'decidim-core/app/models/decidim/user.rb', line 78 def @newsletter_notifications end |
Class Method Details
.download_your_data_images(user) ⇒ Object
194 195 196 |
# File 'decidim-core/app/models/decidim/user.rb', line 194 def self.download_your_data_images(user) user_collection(user).map(&:avatar) end |
.export_serializer ⇒ Object
190 191 192 |
# File 'decidim-core/app/models/decidim/user.rb', line 190 def self.export_serializer Decidim::DownloadYourDataSerializers::DownloadYourDataUserSerializer end |
.find_for_authentication(warden_conditions) ⇒ Object
Check if the user exists with the given email and the current organization
warden_conditions - A hash with the authentication conditions
* email - a String that represents user's email.
* env - A Hash containing environment variables.
Returns a User.
178 179 180 181 182 183 184 |
# File 'decidim-core/app/models/decidim/user.rb', line 178 def self.find_for_authentication(warden_conditions) organization = warden_conditions.dig(:env, "decidim.current_organization") find_by( email: warden_conditions[:email].to_s.downcase, decidim_organization_id: organization.id ) end |
.has_pending_invitations?(organization_id, email) ⇒ Boolean
Returns the user corresponding to the given email
if it exists and has pending invitations,
otherwise returns nil.
109 110 111 |
# File 'decidim-core/app/models/decidim/user.rb', line 109 def self.has_pending_invitations?(organization_id, email) invitation_not_accepted.find_by(decidim_organization_id: organization_id, email:) end |
.log_presenter_class_for(_log) ⇒ Object
119 120 121 |
# File 'decidim-core/app/models/decidim/user.rb', line 119 def self.log_presenter_class_for(_log) Decidim::AdminLog::UserPresenter end |
.user_collection(user) ⇒ Object
186 187 188 |
# File 'decidim-core/app/models/decidim/user.rb', line 186 def self.user_collection(user) where(id: user.id) end |
Instance Method Details
#accepted_user_groups ⇒ Object
return the groups where this user has been accepted
234 235 236 |
# File 'decidim-core/app/models/decidim/user.rb', line 234 def accepted_user_groups UserGroups::AcceptedUserGroups.for(self) end |
#accepts_conversation?(user) ⇒ Boolean
Public: whether the user accepts direct messages from another
158 159 160 161 162 |
# File 'decidim-core/app/models/decidim/user.rb', line 158 def accepts_conversation?(user) return follows?(user) if == "followed-only" true end |
#active_role ⇒ Object
Public: Returns the active role of the user
134 135 136 |
# File 'decidim-core/app/models/decidim/user.rb', line 134 def active_role admin ? "admin" : roles.first end |
#admin_terms_accepted? ⇒ Boolean
208 209 210 |
# File 'decidim-core/app/models/decidim/user.rb', line 208 def admin_terms_accepted? admin_terms_accepted_at.present? end |
#after_confirmation ⇒ Object
282 283 284 285 286 287 288 289 290 291 292 |
# File 'decidim-core/app/models/decidim/user.rb', line 282 def after_confirmation return unless organization.send_welcome_notification? Decidim::EventsManager.publish( event: "decidim.events.core.welcome_notification", event_class: WelcomeNotificationEvent, resource: self, affected_users: [self], extra: { force_email: true } ) end |
#authenticatable_salt ⇒ Object
243 244 245 |
# File 'decidim-core/app/models/decidim/user.rb', line 243 def authenticatable_salt "#{super}#{session_token}" end |
#being_impersonated? ⇒ Boolean
217 218 219 |
# File 'decidim-core/app/models/decidim/user.rb', line 217 def being_impersonated? ImpersonationLog.active.exists?(user: self) end |
#deleted? ⇒ Boolean
Check if the user account has been deleted or not
144 145 146 |
# File 'decidim-core/app/models/decidim/user.rb', line 144 def deleted? deleted_at.present? end |
#follows?(followable) ⇒ Boolean
153 154 155 |
# File 'decidim-core/app/models/decidim/user.rb', line 153 def follows?(followable) Decidim::Follow.where(user: self, followable:).any? end |
#interested_scopes ⇒ Object
225 226 227 |
# File 'decidim-core/app/models/decidim/user.rb', line 225 def interested_scopes @interested_scopes ||= organization.scopes.where(id: interested_scopes_ids) end |
#interested_scopes_ids ⇒ Object
221 222 223 |
# File 'decidim-core/app/models/decidim/user.rb', line 221 def interested_scopes_ids extended_data["interested_scopes"] || [] end |
#invalidate_all_sessions! ⇒ Object
247 248 249 250 |
# File 'decidim-core/app/models/decidim/user.rb', line 247 def invalidate_all_sessions! self.session_token = SecureRandom.hex save! end |
#invitation_pending? ⇒ Boolean
103 104 105 |
# File 'decidim-core/app/models/decidim/user.rb', line 103 def invitation_pending? invited_to_sign_up? && !invitation_accepted? end |
#manageable_user_groups ⇒ Object
return the groups where this user has admin permissions
239 240 241 |
# File 'decidim-core/app/models/decidim/user.rb', line 239 def manageable_user_groups UserGroups::ManageableUserGroups.for(self) end |
#moderator? ⇒ Boolean
274 275 276 277 278 279 280 |
# File 'decidim-core/app/models/decidim/user.rb', line 274 def moderator? Decidim.participatory_space_manifests.map do |manifest| participatory_space_type = manifest.model_class_name.constantize return true if participatory_space_type.moderators(organization).exists?(id:) end false end |
#name ⇒ Object
Public: returns the user’s name or the default one
139 140 141 |
# File 'decidim-core/app/models/decidim/user.rb', line 139 def name super || I18n.t("decidim.anonymous_user") end |
#needs_password_update? ⇒ Boolean
264 265 266 267 268 269 270 271 272 |
# File 'decidim-core/app/models/decidim/user.rb', line 264 def needs_password_update? return false if organization.users_registration_mode == "disabled" return false unless admin? return false unless Decidim.config.admin_password_strong return false if Decidim.config.admin_password_expiration_days.zero? return identities.none? if password_updated_at.blank? password_updated_at < Decidim.config.admin_password_expiration_days.days.ago end |
#notifications_subscriptions ⇒ Object
260 261 262 |
# File 'decidim-core/app/models/decidim/user.rb', line 260 def notifications_subscriptions notification_settings.fetch("subscriptions", {}) end |
#officialized? ⇒ Boolean
Public: whether the user has been officialized or not
149 150 151 |
# File 'decidim-core/app/models/decidim/user.rb', line 149 def officialized? !officialized_at.nil? end |
#presenter ⇒ Object
Returns the presenter for this author, to be used in the views. Required by ActsAsAuthor.
115 116 117 |
# File 'decidim-core/app/models/decidim/user.rb', line 115 def presenter Decidim::UserPresenter.new(self) end |
#role?(role) ⇒ Boolean
Checks if the user has the given ‘role` or not.
role - a String or a Symbol that represents the role that is being
checked
Returns a boolean.
129 130 131 |
# File 'decidim-core/app/models/decidim/user.rb', line 129 def role?(role) roles.include?(role.to_s) end |
#tos_accepted? ⇒ Boolean
198 199 200 201 202 203 204 205 206 |
# File 'decidim-core/app/models/decidim/user.rb', line 198 def tos_accepted? return true if managed return false if accepted_tos_version.nil? # For some reason, if we do not use `#to_i` here we get some # cases where the comparison returns false, but calling `#to_i` returns # the same number :/ accepted_tos_version.to_i >= organization.tos_version.to_i end |
#unread_conversations ⇒ Object
164 165 166 |
# File 'decidim-core/app/models/decidim/user.rb', line 164 def unread_conversations Decidim::Messaging::Conversation.unread_by(self) end |
#unread_messages_count ⇒ Object
168 169 170 |
# File 'decidim-core/app/models/decidim/user.rb', line 168 def @unread_messages_count ||= Decidim::Messaging::Receipt.unread_count(self) end |
#user_invited? ⇒ Boolean
93 94 95 |
# File 'decidim-core/app/models/decidim/user.rb', line 93 def user_invited? invitation_token_changed? && invitation_accepted_at_changed? end |
#user_name ⇒ Object
229 230 231 |
# File 'decidim-core/app/models/decidim/user.rb', line 229 def user_name extended_data["user_name"] || name end |
#verifiable? ⇒ Boolean
Whether this user can be verified against some authorization or not.
213 214 215 |
# File 'decidim-core/app/models/decidim/user.rb', line 213 def verifiable? confirmed? || managed? || being_impersonated? end |