Class: Decidim::DirectVerifications::UserProcessor

Inherits:
Object
  • Object
show all
Defined in:
lib/decidim/direct_verifications/user_processor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(organization, current_user) ⇒ UserProcessor

Returns a new instance of UserProcessor.



6
7
8
9
10
11
12
13
# File 'lib/decidim/direct_verifications/user_processor.rb', line 6

def initialize(organization, current_user)
  @organization = organization
  @current_user = current_user
  @authorization_handler = :direct_verifications
  @errors = { registered: [], authorized: [], revoked: [] }
  @processed = { registered: [], authorized: [], revoked: [] }
  @emails = {}
end

Instance Attribute Details

#authorization_handlerObject

Returns the value of attribute authorization_handler.



16
17
18
# File 'lib/decidim/direct_verifications/user_processor.rb', line 16

def authorization_handler
  @authorization_handler
end

#current_userObject (readonly)

Returns the value of attribute current_user.



15
16
17
# File 'lib/decidim/direct_verifications/user_processor.rb', line 15

def current_user
  @current_user
end

#emailsObject

Returns the value of attribute emails.



15
16
17
# File 'lib/decidim/direct_verifications/user_processor.rb', line 15

def emails
  @emails
end

#errorsObject (readonly)

Returns the value of attribute errors.



15
16
17
# File 'lib/decidim/direct_verifications/user_processor.rb', line 15

def errors
  @errors
end

#organizationObject (readonly)

Returns the value of attribute organization.



15
16
17
# File 'lib/decidim/direct_verifications/user_processor.rb', line 15

def organization
  @organization
end

#processedObject (readonly)

Returns the value of attribute processed.



15
16
17
# File 'lib/decidim/direct_verifications/user_processor.rb', line 15

def processed
  @processed
end

Instance Method Details

#authorize_usersObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/decidim/direct_verifications/user_processor.rb', line 42

def authorize_users
  @emails.each do |email, _name|
    if (u = find_user(email))
      auth = authorization(u)
      next if auth.granted?
      Verification::ConfirmUserAuthorization.call(auth, authorize_form(u)) do
        on(:ok) do
          add_processed :authorized, email
        end
        on(:invalid) do
          add_error :authorized, email
        end
      end
    else
      add_error :authorized, email
    end
  end
end

#register_usersObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/decidim/direct_verifications/user_processor.rb', line 22

def register_users
  @emails.each do |email, name|
    next if find_user(email)
    form = register_form(email, name)
    begin
      InviteUser.call(form) do
        on(:ok) do
          add_processed :registered, email
          log_action find_user(email)
        end
        on(:invalid) do
          add_error :registered, email
        end
      end
    rescue StandardError
      add_error :registered, email
    end
  end
end

#revoke_usersObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/decidim/direct_verifications/user_processor.rb', line 61

def revoke_users
  @emails.each do |email, _name|
    if (u = find_user(email))
      auth = authorization(u)
      next unless auth.granted?
      Verification::DestroyUserAuthorization.call(auth) do
        on(:ok) do
          add_processed :revoked, email
        end
        on(:invalid) do
          add_error :revoked, email
        end
      end
    else
      add_error :revoked, email
    end
  end
end

#total(type) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/decidim/direct_verifications/user_processor.rb', line 80

def total(type)
  if type == :registered
    return User.where(email: @emails.keys, decidim_organization_id: @organization.id)
               .count
  end
  if type == :unconfirmed
    return User.where(email: @emails.keys, decidim_organization_id: @organization.id)
               .where(confirmed_at: nil).count
  end
  if type == :authorized
    return Decidim::Authorization.joins(:user)
                                 .where(name: authorization_handler)
                                 .where("decidim_users.email IN (:emails) AND decidim_users.decidim_organization_id=:org",
                                        emails: @emails.keys, org: @organization.id).count
  end
  0
end