File: close_account.rb

package info (click to toggle)
ruby-rodauth 2.42.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 812 kB
  • sloc: ruby: 7,524; javascript: 100; makefile: 4
file content (89 lines) | stat: -rw-r--r-- 1,977 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# frozen-string-literal: true

module Rodauth
  Feature.define(:close_account, :CloseAccount) do
    notice_flash 'Your account has been closed'
    error_flash 'There was an error closing your account'
    loaded_templates %w'close-account password-field'
    view 'close-account', 'Close Account'
    additional_form_tags
    button 'Close Account'
    after
    before
    redirect
    response

    auth_value_method :account_closed_status_value, 3

    auth_value_methods(
      :close_account_requires_password?,
      :delete_account_on_close?
    )

    auth_methods(
      :close_account,
      :delete_account
    )

    internal_request_method

    route do |r|
      require_account
      before_close_account_route

      r.get do
        close_account_view
      end

      r.post do
        catch_error do
          if close_account_requires_password? && !password_match?(param(password_param))
            throw_error_reason(:invalid_password, invalid_password_error_status, password_param, invalid_password_message)
          end

          transaction do
            before_close_account
            close_account
            after_close_account
            clear_session
            clear_tokens(:close_account)
            if delete_account_on_close?
              delete_account
            end
          end

          close_account_response
        end

        set_error_flash close_account_error_flash
        close_account_view
      end
    end

    def close_account_requires_password?
      modifications_require_password?
    end

    def close_account
      unless skip_status_checks?
        update_account(account_status_column=>account_closed_status_value)
      end

      unless account_password_hash_column
        password_hash_ds.delete
      end
    end

    def delete_account
      account_ds.delete
    end

    def delete_account_on_close?
      skip_status_checks?
    end

    def skip_status_checks?
      false
    end
  end
end