Module: EnjuLeaf::EnjuUser::InstanceMethods
- Defined in:
- lib/enju_leaf/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
ユーザが有効期限切れかどうかをチェックし、期限切れであれば使用不可に設定します。
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_destroy ⇒ 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
ユーザが削除可能かどうかをチェックします。
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
ユーザが有効期限切れかどうかをチェックします。
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
ユーザが特定の権限を持っているかどうかをチェックします。
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
ユーザが管理者かどうかをチェックします。
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権限ユーザかどうかをチェックします。
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
ユーザにパスワードが必要かどうかをチェックします。
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_instructions ⇒ Object
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_password ⇒ String
ユーザに自動生成されたパスワードを設定します。
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_confirmation ⇒ Object
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_information ⇒ Object
ユーザに使用不可の設定を反映させます。
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 |