Module: EnjuSeed::EnjuUser

Extended by:
ActiveSupport::Concern
Defined in:
app/models/concerns/enju_seed/enju_user.rb

Instance Method Summary collapse

Instance Method Details

#check_expirationObject

ユーザが有効期限切れかどうかをチェックし、期限切れであれば使用不可に設定します。

Returns:

  • (Object)


166
167
168
169
170
171
172
173
# File 'app/models/concerns/enju_seed/enju_user.rb', line 166

def check_expiration
  return if has_role?('Administrator')
  if expired_at
    if expired_at.beginning_of_day < Time.zone.now.beginning_of_day
      lock_access! if active_for_authentication?
    end
  end
end

#check_role_before_destroyObject

ユーザの削除前に、管理者ユーザが不在にならないかどうかをチェックします。

Returns:

  • (Object)


177
178
179
180
181
182
183
# File 'app/models/concerns/enju_seed/enju_user.rb', line 177

def check_role_before_destroy
  if has_role?('Administrator')
    if Role.where(name: 'Administrator').first.users.count == 1
      raise username + 'This is the last administrator in this system.'
    end
  end
end

#deletable_by?(current_user) ⇒ Object

ユーザが削除可能かどうかをチェックします。

Parameters:

  • current_user (User)

    ユーザ

Returns:

  • (Object)


225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'app/models/concerns/enju_seed/enju_user.rb', line 225

def deletable_by?(current_user)
  return nil unless current_user
  if defined?(EnjuCirculation)
    # 未返却の資料のあるユーザを削除しようとした
    if checkouts.count > 0
      errors[:base] << I18n.t('user.this_user_has_checked_out_item')
    end
  end

  if has_role?('Librarian')
    # 管理者以外のユーザが図書館員を削除しようとした。図書館員の削除は管理者しかできない
    unless current_user.has_role?('Administrator')
      errors[:base] << I18n.t('user.only_administrator_can_destroy')
    end
    # 最後の図書館員を削除しようとした
    if last_librarian?
      errors[:base] << I18n.t('user.last_librarian')
    end
  end

  # 最後の管理者を削除しようとした
  if has_role?('Administrator')
    if Role.where(name: 'Administrator').first.users.count == 1
      errors[:base] << I18n.t('user.last_administrator')
    end
  end

  if errors[:base] == []
    true
  else
    false
  end
end

#expired?Boolean

ユーザが有効期限切れかどうかをチェックします。

Returns:

  • (Boolean)


195
196
197
198
199
# File 'app/models/concerns/enju_seed/enju_user.rb', line 195

def expired?
  if expired_at
    true if expired_at.beginning_of_day < Time.zone.now.beginning_of_day
  end
end

#has_role?(role_in_question) ⇒ Boolean

ユーザが特定の権限を持っているかどうかをチェックします。

Parameters:

  • role_in_question (String)

    権限名

Returns:

  • (Boolean)


135
136
137
138
139
140
141
142
143
144
145
146
# File 'app/models/concerns/enju_seed/enju_user.rb', line 135

def has_role?(role_in_question)
  return false unless role
  return true if role.name == role_in_question
  case role.name
  when 'Administrator'
    return true
  when 'Librarian'
    return true if role_in_question == 'User'
  else
    false
  end
end

#is_admin?Boolean

ユーザが管理者かどうかをチェックします。

Returns:

  • (Boolean)


203
204
205
206
# File 'app/models/concerns/enju_seed/enju_user.rb', line 203

def is_admin?
  return true if has_role?('Administrator')
  false
end

#last_librarian?Boolean

ユーザがシステム上の最後のLibrarian権限ユーザかどうかをチェックします。

Returns:

  • (Boolean)


210
211
212
213
214
215
216
# File 'app/models/concerns/enju_seed/enju_user.rb', line 210

def last_librarian?
  if has_role?('Librarian')
    role = Role.where(name: 'Librarian').first
    return true if role.users.count == 1
    false
  end
end

#password_required?Boolean

ユーザにパスワードが必要かどうかをチェックします。

Returns:

  • (Boolean)


126
127
128
129
130
# File 'app/models/concerns/enju_seed/enju_user.rb', line 126

def password_required?
  if Devise.mappings[:user].modules.include?(:database_authenticatable)
    !persisted? || !password.nil? || !password_confirmation.nil?
  end
end

#send_confirmation_instructionsObject



218
219
220
# File 'app/models/concerns/enju_seed/enju_user.rb', line 218

def send_confirmation_instructions
  Devise::Mailer.confirmation_instructions(self).deliver if email.present?
end

#set_auto_generated_passwordString

ユーザに自動生成されたパスワードを設定します。

Returns:



187
188
189
190
191
# File 'app/models/concerns/enju_seed/enju_user.rb', line 187

def set_auto_generated_password
  password = Devise.friendly_token[0..7]
  self.password = password
  self.password_confirmation = password
end

#set_confirmationObject



157
158
159
160
161
162
# File 'app/models/concerns/enju_seed/enju_user.rb', line 157

def set_confirmation
  if respond_to?(:confirm!)
    reload
    confirm!
  end
end

#set_lock_informationObject

ユーザに使用不可の設定を反映させます。



149
150
151
152
153
154
155
# File 'app/models/concerns/enju_seed/enju_user.rb', line 149

def set_lock_information
  if locked == '1' and self.active_for_authentication?
    lock_access!
  elsif locked == '0' and !self.active_for_authentication?
    unlock_access!
  end
end