Refactor admin users page, add quick stats
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Râu Cao 2023-02-13 16:32:28 +08:00
parent dd482d7f2e
commit d9e767298b
Signed by: raucao
GPG Key ID: 15E65F399D084BA9
4 changed files with 60 additions and 62 deletions

View File

@ -2,43 +2,18 @@ class Admin::LdapUsersController < Admin::BaseController
before_action :set_current_section before_action :set_current_section
def index def index
attributes = %w{dn cn uid mail admin} ldap = LdapService.new
filter = Net::LDAP::Filter.eq("uid", "*")
@ou = params[:ou] || "kosmos.org" @ou = params[:ou] || "kosmos.org"
treebase = "ou=#{@ou},cn=users,dc=kosmos,dc=org" @orgs = ldap.fetch_organizations
@entries = ldap.fetch_users(ou: @ou)
entries = ldap_client.search(base: treebase, filter: filter, attributes: attributes) @stats = {
entries.sort_by! { |e| e.cn[0] } users_confirmed: User.where(ou: @ou).confirmed.count,
users_pending: User.where(ou: @ou).pending.count
@entries = entries.collect do |e| }
{
uid: e.uid.first,
mail: e.try(:mail) ? e.mail.first : nil,
admin: e.try(:admin) ? 'admin' : nil
# password: e.userpassword.first
}
end
# ldap_client.get_operation_result
end end
private private
def ldap_client
ldap_client ||= Net::LDAP.new host: ldap_config['host'],
port: ldap_config['port'],
# encryption: ldap_config['ssl'],
auth: {
method: :simple,
username: ldap_config['admin_user'],
password: ldap_config['admin_password']
}
end
def ldap_config
ldap_config ||= YAML.load(ERB.new(File.read("#{Rails.root}/config/ldap.yml")).result)[Rails.env]
end
def set_current_section def set_current_section
@current_section = :ldap_users @current_section = :ldap_users
end end

View File

@ -15,6 +15,9 @@ class User < ApplicationRecord
validates_uniqueness_of :email validates_uniqueness_of :email
validates :email, email: true validates :email, email: true
scope :confirmed, -> { where.not(confirmed_at: nil) }
scope :pending, -> { where(confirmed_at: nil) }
lockbox_encrypts :ln_login lockbox_encrypts :ln_login
lockbox_encrypts :ln_password lockbox_encrypts :ln_password

View File

@ -1,34 +1,54 @@
<%= render HeaderComponent.new(title: "LDAP Users: #{@ou}") %> <%= render HeaderComponent.new(title: "LDAP Users: #{@ou}") %>
<%= render MainSimpleComponent.new do %> <%= render MainSimpleComponent.new do %>
<h3 class="hidden">Domains</h3> <section>
<ul class="mb-10"> <%= render QuickstatsContainerComponent.new do %>
<li class="inline-block"> <%= render QuickstatsItemComponent.new(
<%= link_to 'kosmos.org', admin_ldap_users_path, class: "ks-text-link" %> type: :number,
</li> title: 'Confirmed',
<li class="inline-block ml-6"> value: @stats[:users_confirmed],
<%= link_to '5apps.com', admin_ldap_users_path(ou: '5apps.com'), class: "ks-text-link" %> ) %>
</li> <%= render QuickstatsItemComponent.new(
</ul> type: :number,
title: 'Pending',
value: @stats[:users_pending],
) %>
<% end %>
</section>
<table> <% if @orgs.length > 1 %>
<thead> <section>
<tr> <h3 class="hidden">Domains</h3>
<th>UID</th> <ul>
<th>E-Mail</th> <% @orgs.each do |org| %>
<th>Admin</th> <li class="inline-block">
<!-- <th>Password</th> --> <%= link_to org[:ou], admin_ldap_users_path(ou: org[:ou]), class: "ks-text-link" %>
</tr> </li>
</thead> <% end %>
<tbody> </ul>
<% @entries.each do |entry| %> </section>
<tr> <% end %>
<td><%= entry[:uid] %></td>
<td><%= entry[:mail] %></td> <section>
<td><%= entry[:admin] %></td> <table>
<!-- <td><%= entry[:password] %></td> --> <thead>
</tr> <tr>
<% end %> <th>UID</th>
</tbody> <th>E-Mail</th>
</table> <th>Admin</th>
<!-- <th>Password</th> -->
</tr>
</thead>
<tbody>
<% @entries.each do |entry| %>
<tr>
<td><%= entry[:uid] %></td>
<td><%= entry[:mail] %></td>
<td><%= entry[:admin] %></td>
<!-- <td><%= entry[:password] %></td> -->
</tr>
<% end %>
</tbody>
</table>
</section>
<% end %> <% end %>

View File

@ -5,7 +5,7 @@ RSpec.describe WalletSummaryComponent, type: :component do
expect( expect(
render_inline(described_class.new(balance: 2301000)) {}.css("section").to_html render_inline(described_class.new(balance: 2301000)) {}.css("section").to_html
).to include( ).to include(
"2,301,000 sats" "2,301,000"
) )
end end
end end