Allow users to set/update their display name in LDAP
Some checks failed
continuous-integration/drone/push Build is failing
continuous-integration/drone/pr Build is failing

This commit is contained in:
Râu Cao 2023-05-27 19:59:49 +02:00
parent 32d1992632
commit f74227fedb
Signed by: raucao
GPG Key ID: 15E65F399D084BA9
9 changed files with 79 additions and 41 deletions

View File

@ -12,12 +12,21 @@ class SettingsController < ApplicationController
end end
def update def update
@user.preferences.merge! user_params[:preferences] @user.preferences.merge!(user_params[:preferences] || {})
@user.save! @user.display_name = user_params[:display_name]
redirect_to setting_path(@settings_section), flash: { if @user.save
success: 'Settings saved.' if @user.display_name && (@user.display_name != @user.ldap_entry[:display_name])
} LdapManager::UpdateDisplayName.call(@user.dn, user_params[:display_name])
end
redirect_to setting_path(@settings_section), flash: {
success: 'Settings saved.'
}
else
@validation_errors = @user.errors
render :show, status: :unprocessable_entity
end
end end
def update_email def update_email
@ -46,30 +55,31 @@ class SettingsController < ApplicationController
private private
def set_main_nav_section def set_main_nav_section
@current_section = :settings @current_section = :settings
end end
def set_settings_section def set_settings_section
@settings_section = params[:section] @settings_section = params[:section]
allowed_sections = [:profile, :account, :lightning, :xmpp] allowed_sections = [:profile, :account, :lightning, :xmpp]
unless allowed_sections.include?(@settings_section.to_sym) unless allowed_sections.include?(@settings_section.to_sym)
redirect_to setting_path(:profile) redirect_to setting_path(:profile)
end
end end
end
def user_params
params.require(:user).permit(preferences: [
:lightning_notify_sats_received,
:xmpp_exchange_contacts_with_invitees
])
end
def set_user def set_user
@user = current_user @user = current_user
end end
def email_params def user_params
params.require(:user).permit(:email, :current_password) params.require(:user).permit(:display_name, preferences: [
end :lightning_notify_sats_received,
:xmpp_exchange_contacts_with_invitees
])
end
def email_params
params.require(:user).permit(:email, :current_password)
end
end end

View File

@ -1,6 +1,8 @@
class User < ApplicationRecord class User < ApplicationRecord
include EmailValidatable include EmailValidatable
attr_accessor :display_name
serialize :preferences, UserPreferences serialize :preferences, UserPreferences
# Relations # Relations
@ -17,7 +19,7 @@ class User < ApplicationRecord
has_many :accounts, through: :lndhub_user has_many :accounts, through: :lndhub_user
validates_uniqueness_of :cn validates_uniqueness_of :cn
validates_length_of :cn, :minimum => 3 validates_length_of :cn, minimum: 3
validates_format_of :cn, with: /\A([a-z0-9\-])*\z/, validates_format_of :cn, with: /\A([a-z0-9\-])*\z/,
if: Proc.new{ |u| u.cn.present? }, if: Proc.new{ |u| u.cn.present? },
message: "is invalid. Please use only letters, numbers and -" message: "is invalid. Please use only letters, numbers and -"
@ -31,6 +33,8 @@ class User < ApplicationRecord
validates_uniqueness_of :email validates_uniqueness_of :email
validates :email, email: true validates :email, email: true
validates_length_of :display_name, minimum: 3, maximum: 35, allow_blank: true
scope :confirmed, -> { where.not(confirmed_at: nil) } scope :confirmed, -> { where.not(confirmed_at: nil) }
scope :pending, -> { where(confirmed_at: nil) } scope :pending, -> { where(confirmed_at: nil) }
@ -115,8 +119,13 @@ class User < ApplicationRecord
@dn = Devise::LDAP::Adapter.get_dn(self.cn) @dn = Devise::LDAP::Adapter.get_dn(self.cn)
end end
def ldap_entry def ldap_entry(reload: false)
ldap.fetch_users(uid: self.cn, ou: self.ou).first return @ldap_entry if defined?(@ldap_entry) && !reload
@ldap_entry = ldap.fetch_users(uid: self.cn, ou: self.ou).first
end
def display_name
@display_name ||= ldap_entry[:display_name]
end end
def services_enabled def services_enabled

View File

@ -0,0 +1,12 @@
module LdapManager
class UpdateDisplayName < LdapManagerService
def initialize(dn, display_name)
@dn = dn
@display_name = display_name
end
def call
replace_attribute @dn, :displayName, @display_name
end
end
end

View File

@ -6,7 +6,7 @@ module LdapManager
end end
def call def call
replace_attribute @dn, :mail, [ @address ] replace_attribute @dn, :mail, @address
end end
end end
end end

View File

@ -50,7 +50,7 @@ class LdapService < ApplicationService
treebase = ldap_config["base"] treebase = ldap_config["base"]
end end
attributes = %w{dn cn uid mail admin service} attributes = %w{dn cn uid mail displayName admin service}
filter = Net::LDAP::Filter.eq("uid", args[:uid] || "*") filter = Net::LDAP::Filter.eq("uid", args[:uid] || "*")
entries = ldap_client.search(base: treebase, filter: filter, attributes: attributes) entries = ldap_client.search(base: treebase, filter: filter, attributes: attributes)
@ -59,6 +59,7 @@ class LdapService < ApplicationService
{ {
uid: e.uid.first, uid: e.uid.first,
mail: e.try(:mail) ? e.mail.first : nil, mail: e.try(:mail) ? e.mail.first : nil,
display_name: e.try(:displayName) ? e.displayName.first : nil,
admin: e.try(:admin) ? 'admin' : nil, admin: e.try(:admin) ? 'admin' : nil,
service: e.try(:service) service: e.try(:service)
} }

View File

@ -34,7 +34,7 @@
<%= f.password_field :current_password, class: "w-full", required: true %> <%= f.password_field :current_password, class: "w-full", required: true %>
</p> </p>
<p class="mt-6"> <p class="mt-6">
<%= f.submit "Update", class: "btn-md btn-blue" %> <%= f.submit "Update", class: "btn-md btn-blue w-full md:w-auto" %>
</p> </p>
</div> </div>
<% end %> <% end %>

View File

@ -21,10 +21,15 @@
<p class="text-sm text-gray-500"> <p class="text-sm text-gray-500">
Your user address for Chat and Lightning Network. Your user address for Chat and Lightning Network.
</p> </p>
<%= form_for(@user, url: setting_path(:profile), html: { :method => :put }) do |f| %>
<%# <%= form_for(@user, as: "profile", url: settings_profile_path) do |f| %> <%= render FormElements::FieldsetComponent.new(tag: "div", title: "Display name") do %>
<%# <p class="mt-8"> <%= f.text_field :display_name, class: "w-full sm:w-3/5 mb-2" %>
<%# <%= f.submit "Save changes", class: 'btn-md btn-blue w-full sm:w-auto' %> <% if @validation_errors.present? && @validation_errors[:display_name].present? %>
<%# </p> <p class="error-msg"><%= @validation_errors[:display_name].first %></p>
<%# <% end %> <% end %>
<% end %>
<p class="mt-8 pt-6 border-t border-gray-200 text-right">
<%= f.submit 'Save', class: "btn-md btn-blue w-full md:w-auto" %>
</p>
<% end %>
</section> </section>

View File

@ -1,20 +1,20 @@
<%= render SidenavLinkComponent.new( <%= render SidenavLinkComponent.new(
name: "Profile", path: setting_path(:profile), icon: "user", name: "Profile", path: setting_path(:profile), icon: "user",
active: current_page?(setting_path(:profile)) active: @settings_section.to_s == "profile"
) %> ) %>
<%= render SidenavLinkComponent.new( <%= render SidenavLinkComponent.new(
name: "Account", path: setting_path(:account), icon: "key", name: "Account", path: setting_path(:account), icon: "key",
active: current_page?(setting_path(:account)) active: @settings_section.to_s == "account"
) %> ) %>
<% if Setting.ejabberd_enabled %> <% if Setting.ejabberd_enabled %>
<%= render SidenavLinkComponent.new( <%= render SidenavLinkComponent.new(
name: "Chat", path: setting_path(:xmpp), icon: "message-circle", name: "Chat", path: setting_path(:xmpp), icon: "message-circle",
active: current_page?(setting_path(:xmpp)) active: @settings_section.to_s == "xmpp"
) %> ) %>
<% end %> <% end %>
<% if Setting.lndhub_enabled %> <% if Setting.lndhub_enabled %>
<%= render SidenavLinkComponent.new( <%= render SidenavLinkComponent.new(
name: "Lightning", path: setting_path(:lightning), icon: "zap", name: "Lightning", path: setting_path(:lightning), icon: "zap",
active: current_page?(setting_path(:lightning)) active: @settings_section.to_s == "lightning"
) %> ) %>
<% end %> <% end %>

View File

@ -28,6 +28,7 @@ Rails.application.routes.draw do
resources :settings, param: 'section', only: ['index', 'show', 'update'] do resources :settings, param: 'section', only: ['index', 'show', 'update'] do
collection do collection do
post 'update_profile'
post 'update_email' post 'update_email'
post 'reset_password' post 'reset_password'
end end