Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Import extern users from init.pp in git #550

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion app/controllers/app_settings_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

class AppSettingsController < ApplicationController
before_action :set_app_setting, only: %i[edit update]
before_action :set_app_setting, only: %i[edit update import]
before_action :authenticate_admin

# GET /app_settings/1/edit
Expand All @@ -16,6 +16,24 @@ def update
end
end

def import
current_users = User.all.collect {|each| each.name}
GitHelper.open_git_repository(for_write: true) {}
initpp = File.read(File.join(Puppetscript.init_path, Puppetscript.init_file_name))
all_initpp_users = initpp.scan(/realname\s*=>\s*'(.+?)'/).flatten
initpp_only_users = all_initpp_users - current_users

successful_created_users = 0

initpp_only_users.each do |each|
user = User.extern each
puts 'enter'
successful_created_users += 1 if user.persisted?
end

redirect_to edit_app_setting_path(@app_setting), notice: "Successfully added #{successful_created_users} users."
end

private

# Use callbacks to share common setup or constraints between actions.
Expand Down
9 changes: 9 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,15 @@ def self.from_omniauth(auth)
end
end

# This is only to get users from a imported init.pp file.
# They are not able to login because the callback checks only
# user with hpi as provider (this is wanted behaviour)
def self.extern(user_name)
first_name, last_name = user_name.scan(/((?:\w|[-äöüÄÖÜß])+) (.*)/).first
create(provider: :extern, first_name: first_name, last_name: last_name,
email: "#{first_name}.#{last_name}@mail.com", password: 'test12345!')
end

def self.from_mail_identifier(mail_id)
all.each do |user|
return user if user.human_readable_identifier.casecmp(mail_id).zero?
Expand Down
3 changes: 2 additions & 1 deletion app/views/app_settings/edit.html.erb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<h1>HART Settings</h1>

<%= render 'form', app_setting: @app_setting %>
<%= render 'form', app_setting: @app_setting %>
<%= button_to 'Import', controller: :app_settings, action: :import %>
6 changes: 5 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@
# '/dashboard'
get '/dashboard' => 'dashboard#index', as: :dashboard
# '/app_settings/...'
resources :app_settings, only: %i[update edit]
resources :app_settings, only: %i[update edit] do
member do
post :import
end
end
# '/projects/...'
resources :projects
# '/servers/...'
Expand Down