Class: Decidim::User

Inherits:
UserBaseEntity show all
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

Constants inherited from UserBaseEntity

Decidim::UserBaseEntity::REGEXP_NAME, Decidim::UserBaseEntity::REGEXP_NICKNAME

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#followers

Instance Attribute Details

#invitation_instructionsObject

Public: Allows customizing the invitation instruction email content when inviting a user.

Returns a String.



94
95
96
# File 'decidim-core/app/models/decidim/user.rb', line 94

def invitation_instructions
  @invitation_instructions
end

#newsletter_notificationsObject

Returns the value of attribute newsletter_notifications.



72
73
74
# File 'decidim-core/app/models/decidim/user.rb', line 72

def newsletter_notifications
  @newsletter_notifications
end

Class Method Details

.download_your_data_images(user) ⇒ Object



206
207
208
# File 'decidim-core/app/models/decidim/user.rb', line 206

def self.download_your_data_images(user)
  user_collection(user).map(&:avatar)
end

.export_serializerObject



202
203
204
# File 'decidim-core/app/models/decidim/user.rb', line 202

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.



190
191
192
193
194
195
196
# File 'decidim-core/app/models/decidim/user.rb', line 190

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

.first_warning_inactive_usersObject

Returns users eligible for receiving the first inactivity warning email.



107
108
109
# File 'decidim-core/app/models/decidim/user.rb', line 107

def self.first_warning_inactive_users
  InactiveUsersQuery.new(self).for_first_warning(Decidim.first_warning_inactive_users_after_days.days.ago)
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.

Returns:

  • (Boolean)


102
103
104
# File 'decidim-core/app/models/decidim/user.rb', line 102

def self.has_pending_invitations?(organization_id, email)
  invitation_not_accepted.find_by(decidim_organization_id: organization_id, email:)
end

.last_warning_inactive_usersObject

Returns users eligible for receiving the final inactivity warning email.



112
113
114
# File 'decidim-core/app/models/decidim/user.rb', line 112

def self.last_warning_inactive_users
  InactiveUsersQuery.new(self).for_last_warning(Decidim.last_warning_inactive_users_after_days.days.ago)
end

.log_presenter_class_for(_log) ⇒ Object



127
128
129
# File 'decidim-core/app/models/decidim/user.rb', line 127

def self.log_presenter_class_for(_log)
  Decidim::AdminLog::UserPresenter
end

.removable_usersObject

Returns users eligible for account removal due to prolonged inactivity.



117
118
119
# File 'decidim-core/app/models/decidim/user.rb', line 117

def self.removable_users
  InactiveUsersQuery.new(self).for_removal(Decidim.delete_inactive_users_last_warning_days_before.days.ago)
end

.user_collection(user) ⇒ Object



198
199
200
# File 'decidim-core/app/models/decidim/user.rb', line 198

def self.user_collection(user)
  where(id: user.id)
end

Instance Method Details

#accepts_conversation?(user) ⇒ Boolean

Public: whether the user accepts direct messages from another

Returns:

  • (Boolean)


170
171
172
173
174
# File 'decidim-core/app/models/decidim/user.rb', line 170

def accepts_conversation?(user)
  return follows?(user) if direct_message_types == "followed-only"

  true
end

#active_roleObject

Public: Returns the active role of the user



142
143
144
# File 'decidim-core/app/models/decidim/user.rb', line 142

def active_role
  admin ? "admin" : roles.first
end

#admin_terms_accepted?Boolean

Returns:

  • (Boolean)


220
221
222
# File 'decidim-core/app/models/decidim/user.rb', line 220

def admin_terms_accepted?
  admin_terms_accepted_at.present?
end

#after_confirmationObject



280
281
282
283
284
285
286
287
288
289
290
# File 'decidim-core/app/models/decidim/user.rb', line 280

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_saltObject



237
238
239
# File 'decidim-core/app/models/decidim/user.rb', line 237

def authenticatable_salt
  "#{super}#{session_token}"
end

#being_impersonated?Boolean

Returns:

  • (Boolean)


229
230
231
# File 'decidim-core/app/models/decidim/user.rb', line 229

def being_impersonated?
  ImpersonationLog.active.exists?(user: self)
end

#deleted?Boolean

Check if the user account has been deleted or not

Returns:

  • (Boolean)


152
153
154
# File 'decidim-core/app/models/decidim/user.rb', line 152

def deleted?
  deleted_at.present?
end

#ephemeral?Boolean

Returns:

  • (Boolean)


276
277
278
# File 'decidim-core/app/models/decidim/user.rb', line 276

def ephemeral?
  extended_data["ephemeral"]
end

#follows?(followable) ⇒ Boolean

Returns:

  • (Boolean)


165
166
167
# File 'decidim-core/app/models/decidim/user.rb', line 165

def follows?(followable)
  Decidim::Follow.where(user: self, followable:).any?
end

#group?Boolean

Returns:

  • (Boolean)


161
162
163
# File 'decidim-core/app/models/decidim/user.rb', line 161

def group?
  extended_data["group"]
end

#invalidate_all_sessions!Object



241
242
243
244
# File 'decidim-core/app/models/decidim/user.rb', line 241

def invalidate_all_sessions!
  self.session_token = SecureRandom.hex
  save!
end

#invitation_pending?Boolean

Returns:

  • (Boolean)


96
97
98
# File 'decidim-core/app/models/decidim/user.rb', line 96

def invitation_pending?
  invited_to_sign_up? && !invitation_accepted?
end

#moderator?Boolean

Returns:

  • (Boolean)


268
269
270
271
272
273
274
# File 'decidim-core/app/models/decidim/user.rb', line 268

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

#nameObject

Public: returns the user’s name or the default one



147
148
149
# File 'decidim-core/app/models/decidim/user.rb', line 147

def name
  super || I18n.t("decidim.anonymous_user")
end

#needs_password_update?Boolean

Returns:

  • (Boolean)


258
259
260
261
262
263
264
265
266
# File 'decidim-core/app/models/decidim/user.rb', line 258

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_subscriptionsObject



254
255
256
# File 'decidim-core/app/models/decidim/user.rb', line 254

def notifications_subscriptions
  notification_settings.fetch("subscriptions", {})
end

#officialized?Boolean

Public: whether the user has been officialized or not

Returns:

  • (Boolean)


157
158
159
# File 'decidim-core/app/models/decidim/user.rb', line 157

def officialized?
  !officialized_at.nil?
end

#presenterObject

Returns the presenter for this author, to be used in the views. Required by ActsAsAuthor.



123
124
125
# File 'decidim-core/app/models/decidim/user.rb', line 123

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.

Returns:

  • (Boolean)


137
138
139
# File 'decidim-core/app/models/decidim/user.rb', line 137

def role?(role)
  roles.include?(role.to_s)
end

#tos_accepted?Boolean

Returns:

  • (Boolean)


210
211
212
213
214
215
216
217
218
# File 'decidim-core/app/models/decidim/user.rb', line 210

def tos_accepted?
  return true if managed && !ephemeral?
  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_conversationsObject



176
177
178
# File 'decidim-core/app/models/decidim/user.rb', line 176

def unread_conversations
  Decidim::Messaging::Conversation.unread_by(self)
end

#unread_messages_countObject



180
181
182
# File 'decidim-core/app/models/decidim/user.rb', line 180

def unread_messages_count
  @unread_messages_count ||= Decidim::Messaging::Receipt.unread_count(self)
end

#user_invited?Boolean

Returns:

  • (Boolean)


86
87
88
# File 'decidim-core/app/models/decidim/user.rb', line 86

def user_invited?
  invitation_token_changed? && invitation_accepted_at_changed?
end

#user_nameObject



233
234
235
# File 'decidim-core/app/models/decidim/user.rb', line 233

def user_name
  extended_data["user_name"] || name
end

#verifiable?Boolean

Whether this user can be verified against some authorization or not.

Returns:

  • (Boolean)


225
226
227
# File 'decidim-core/app/models/decidim/user.rb', line 225

def verifiable?
  confirmed? || managed? || being_impersonated?
end