Module: EnjuLeaf::EnjuUser::InstanceMethods

Defined in:
lib/enju_leaf/user.rb

Instance Method Summary collapse

Instance Method Details

#check_expirationObject

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

Returns:

  • (Object)


176
177
178
179
180
181
182
183
# File 'lib/enju_leaf/user.rb', line 176

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)


187
188
189
190
191
192
193
# File 'lib/enju_leaf/user.rb', line 187

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)


235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/enju_leaf/user.rb', line 235

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)


205
206
207
208
209
# File 'lib/enju_leaf/user.rb', line 205

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)


145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/enju_leaf/user.rb', line 145

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)


213
214
215
216
# File 'lib/enju_leaf/user.rb', line 213

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

#last_librarian?Boolean

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

Returns:

  • (Boolean)


220
221
222
223
224
225
226
# File 'lib/enju_leaf/user.rb', line 220

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)


136
137
138
139
140
# File 'lib/enju_leaf/user.rb', line 136

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

#send_confirmation_instructionsObject



228
229
230
# File 'lib/enju_leaf/user.rb', line 228

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

#set_auto_generated_passwordString

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

Returns:



197
198
199
200
201
# File 'lib/enju_leaf/user.rb', line 197

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

#set_confirmationObject



167
168
169
170
171
172
# File 'lib/enju_leaf/user.rb', line 167

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

#set_lock_informationObject

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



159
160
161
162
163
164
165
# File 'lib/enju_leaf/user.rb', line 159

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