Module: EnjuSeed::EnjuUser
- Extended by:
- ActiveSupport::Concern
- Defined in:
- app/models/concerns/enju_seed/enju_user.rb
Instance Method Summary collapse
-
#check_expiration ⇒ Object
ユーザが有効期限切れかどうかをチェックし、期限切れであれば使用不可に設定します。.
-
#check_role_before_destroy ⇒ Object
ユーザの削除前に、管理者ユーザが不在にならないかどうかをチェックします。.
-
#deletable_by?(current_user) ⇒ Object
ユーザが削除可能かどうかをチェックします。.
-
#expired? ⇒ Boolean
ユーザが有効期限切れかどうかをチェックします。.
-
#has_role?(role_in_question) ⇒ Boolean
ユーザが特定の権限を持っているかどうかをチェックします。.
-
#is_admin? ⇒ Boolean
ユーザが管理者かどうかをチェックします。.
-
#last_librarian? ⇒ Boolean
ユーザがシステム上の最後のLibrarian権限ユーザかどうかをチェックします。.
-
#password_required? ⇒ Boolean
ユーザにパスワードが必要かどうかをチェックします。.
- #send_confirmation_instructions ⇒ Object
-
#set_auto_generated_password ⇒ String
ユーザに自動生成されたパスワードを設定します。.
- #set_confirmation ⇒ Object
-
#set_lock_information ⇒ Object
ユーザに使用不可の設定を反映させます。.
Instance Method Details
#check_expiration ⇒ Object
ユーザが有効期限切れかどうかをチェックし、期限切れであれば使用不可に設定します。
169 170 171 172 173 174 175 176 |
# File 'app/models/concerns/enju_seed/enju_user.rb', line 169 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_destroy ⇒ Object
ユーザの削除前に、管理者ユーザが不在にならないかどうかをチェックします。
180 181 182 183 184 185 186 |
# File 'app/models/concerns/enju_seed/enju_user.rb', line 180 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
ユーザが削除可能かどうかをチェックします。
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 258 259 260 |
# File 'app/models/concerns/enju_seed/enju_user.rb', line 228 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
ユーザが有効期限切れかどうかをチェックします。
198 199 200 201 202 |
# File 'app/models/concerns/enju_seed/enju_user.rb', line 198 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
ユーザが特定の権限を持っているかどうかをチェックします。
138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'app/models/concerns/enju_seed/enju_user.rb', line 138 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
ユーザが管理者かどうかをチェックします。
206 207 208 209 |
# File 'app/models/concerns/enju_seed/enju_user.rb', line 206 def is_admin? return true if has_role?('Administrator') false end |
#last_librarian? ⇒ Boolean
ユーザがシステム上の最後のLibrarian権限ユーザかどうかをチェックします。
213 214 215 216 217 218 219 |
# File 'app/models/concerns/enju_seed/enju_user.rb', line 213 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
ユーザにパスワードが必要かどうかをチェックします。
129 130 131 132 133 |
# File 'app/models/concerns/enju_seed/enju_user.rb', line 129 def password_required? if Devise.mappings[:user].modules.include?(:database_authenticatable) !persisted? || !password.nil? || !password_confirmation.nil? end end |
#send_confirmation_instructions ⇒ Object
221 222 223 |
# File 'app/models/concerns/enju_seed/enju_user.rb', line 221 def send_confirmation_instructions Devise::Mailer.confirmation_instructions(self).deliver if email.present? end |
#set_auto_generated_password ⇒ String
ユーザに自動生成されたパスワードを設定します。
190 191 192 193 194 |
# File 'app/models/concerns/enju_seed/enju_user.rb', line 190 def set_auto_generated_password password = Devise.friendly_token[0..7] self.password = password self.password_confirmation = password end |
#set_confirmation ⇒ Object
160 161 162 163 164 165 |
# File 'app/models/concerns/enju_seed/enju_user.rb', line 160 def set_confirmation if respond_to?(:confirm!) reload confirm! end end |
#set_lock_information ⇒ Object
ユーザに使用不可の設定を反映させます。
152 153 154 155 156 157 158 |
# File 'app/models/concerns/enju_seed/enju_user.rb', line 152 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 |