Class: Decidim::User

Inherits:
UserBaseEntity show all
Includes:
ActsAsAuthor, DownloadYourData, Searchable, Traceable, UserReportable
Defined in:
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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Searchable

searchable_resources, 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

#public_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.



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

def invitation_instructions
  @invitation_instructions
end

#newsletter_notificationsObject

Returns the value of attribute newsletter_notifications.



79
80
81
# File 'app/models/decidim/user.rb', line 79

def newsletter_notifications
  @newsletter_notifications
end

Class Method Details

.download_your_data_images(user) ⇒ Object



195
196
197
# File 'app/models/decidim/user.rb', line 195

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

.export_serializerObject



191
192
193
# File 'app/models/decidim/user.rb', line 191

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.



179
180
181
182
183
184
185
# File 'app/models/decidim/user.rb', line 179

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.

Returns:

  • (Boolean)


110
111
112
# File 'app/models/decidim/user.rb', line 110

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

.log_presenter_class_for(_log) ⇒ Object



120
121
122
# File 'app/models/decidim/user.rb', line 120

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

.user_collection(user) ⇒ Object



187
188
189
# File 'app/models/decidim/user.rb', line 187

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

Instance Method Details

#accepted_user_groupsObject

return the groups where this user has been accepted



235
236
237
# File 'app/models/decidim/user.rb', line 235

def accepted_user_groups
  UserGroups::AcceptedUserGroups.for(self)
end

#accepts_conversation?(user) ⇒ Boolean

Public: whether the user accepts direct messages from another

Returns:

  • (Boolean)


159
160
161
162
163
# File 'app/models/decidim/user.rb', line 159

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



135
136
137
# File 'app/models/decidim/user.rb', line 135

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

#admin_terms_accepted?Boolean

Returns:

  • (Boolean)


209
210
211
# File 'app/models/decidim/user.rb', line 209

def admin_terms_accepted?
  return true if admin_terms_accepted_at
end

#authenticatable_saltObject



244
245
246
# File 'app/models/decidim/user.rb', line 244

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

#being_impersonated?Boolean

Returns:

  • (Boolean)


218
219
220
# File 'app/models/decidim/user.rb', line 218

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

#deleted?Boolean

Check if the user account has been deleted or not

Returns:

  • (Boolean)


145
146
147
# File 'app/models/decidim/user.rb', line 145

def deleted?
  deleted_at.present?
end

#follows?(followable) ⇒ Boolean

Returns:

  • (Boolean)


154
155
156
# File 'app/models/decidim/user.rb', line 154

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

#interested_scopesObject



226
227
228
# File 'app/models/decidim/user.rb', line 226

def interested_scopes
  @interested_scopes ||= organization.scopes.where(id: interested_scopes_ids)
end

#interested_scopes_idsObject



222
223
224
# File 'app/models/decidim/user.rb', line 222

def interested_scopes_ids
  extended_data["interested_scopes"] || []
end

#invalidate_all_sessions!Object



248
249
250
251
# File 'app/models/decidim/user.rb', line 248

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

#invitation_pending?Boolean

Returns:

  • (Boolean)


104
105
106
# File 'app/models/decidim/user.rb', line 104

def invitation_pending?
  invited_to_sign_up? && !invitation_accepted?
end

#manageable_user_groupsObject

return the groups where this user has admin permissions



240
241
242
# File 'app/models/decidim/user.rb', line 240

def manageable_user_groups
  UserGroups::ManageableUserGroups.for(self)
end

#nameObject

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



140
141
142
# File 'app/models/decidim/user.rb', line 140

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

#needs_password_update?Boolean

Returns:

  • (Boolean)


265
266
267
268
269
270
271
# File 'app/models/decidim/user.rb', line 265

def needs_password_update?
  return false unless admin?
  return false unless Decidim.config.admin_password_strong
  return true if password_updated_at.blank?

  password_updated_at < Decidim.config.admin_password_expiration_days.days.ago
end

#notifications_subscriptionsObject



261
262
263
# File 'app/models/decidim/user.rb', line 261

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

#officialized?Boolean

Public: whether the user has been officialized or not

Returns:

  • (Boolean)


150
151
152
# File 'app/models/decidim/user.rb', line 150

def officialized?
  !officialized_at.nil?
end

#presenterObject

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



116
117
118
# File 'app/models/decidim/user.rb', line 116

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)


130
131
132
# File 'app/models/decidim/user.rb', line 130

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

#tos_accepted?Boolean

Returns:

  • (Boolean)


199
200
201
202
203
204
205
206
207
# File 'app/models/decidim/user.rb', line 199

def tos_accepted?
  return true if managed
  return false if accepted_tos_version.nil?

  # For some reason, if we don't 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



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

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

#unread_messages_countObject



169
170
171
# File 'app/models/decidim/user.rb', line 169

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

#user_invited?Boolean

Returns:

  • (Boolean)


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

def user_invited?
  invitation_token_changed? && invitation_accepted_at_changed?
end

#user_nameObject



230
231
232
# File 'app/models/decidim/user.rb', line 230

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

#verifiable?Boolean

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

Returns:

  • (Boolean)


214
215
216
# File 'app/models/decidim/user.rb', line 214

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