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

Added api_user and white_ip management to account view #96

Merged
merged 14 commits into from
Jun 6, 2023
Merged
16 changes: 7 additions & 9 deletions .github/workflows/ruby.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ jobs:
strategy:
fail-fast: false
matrix:
os: [ubuntu-18.04]
os: [ ubuntu-22.04 ]
ruby: [ '3.2.0' ]
runs-on: ${{ matrix.os }}
continue-on-error: ${{ endsWith(matrix.ruby, 'head') || matrix.ruby == 'debug' }}
Expand All @@ -24,13 +24,11 @@ jobs:
bundle env
head -n1 $(which bundle)

- name: Install wkhtmlpdf (push)
- name: Install wkhtmltopdf (push)
run: |
wget http://se.archive.ubuntu.com/ubuntu/pool/main/libp/libpng/libpng12-0_1.2.54-1ubuntu1_amd64.deb
sudo dpkg -i libpng12-0_1.2.54-1ubuntu1_amd64.deb
wget https://github.com/wkhtmltopdf/wkhtmltopdf/releases/download/0.12.5/wkhtmltox_0.12.5-1.xenial_amd64.deb
sudo apt-get install -y gdebi
sudo gdebi --non-interactive wkhtmltox_0.12.5-1.xenial_amd64.deb
sudo apt-get install -y xfonts-base xfonts-75dpi
wget https://github.com/wkhtmltopdf/packaging/releases/download/0.12.6.1-2/wkhtmltox_0.12.6.1-2.jammy_amd64.deb
sudo dpkg -i wkhtmltox_0.12.6.1-2.jammy_amd64.deb
if: github.event_name == 'push'

- name: Set ENV for codeclimate (pull_request)
Expand Down Expand Up @@ -79,9 +77,9 @@ jobs:
strategy:
fail-fast: false
matrix:
os: [ ubuntu-18.04 ]
os: [ ubuntu-22.04 ]
ruby: [ '3.2.0' ]
runs-on: ubuntu-18.04
runs-on: ubuntu-22.04

env:
CC_TEST_REPORTER_ID: ${{ secrets.CC_TEST_REPORTER_ID }}
Expand Down
7 changes: 1 addition & 6 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM --platform=linux/amd64 ruby:3.1.3
FROM --platform=linux/amd64 ruby:3.2.0

RUN apt-get update && apt-get install -y --no-install-recommends \
npm \
Expand All @@ -12,13 +12,8 @@ WORKDIR /opt/webapps/app

COPY Rakefile Gemfile Gemfile.lock ./

# Use this for local gem troubleshooting:
# ADD vendor/gems/omniauth-tara ./vendor/gems/omniauth-tara
# ADD vendor/gems/omniauth ./vendor/gems/omniauth

RUN gem install bundler && bundle install --jobs 20 --retry 5

COPY package.json yarn.lock ./

# RUN yarn cache clean
RUN yarn install --check-files && yarn sass-migrator division **/*.scss
13 changes: 8 additions & 5 deletions Dockerfile.dev
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@ RUN npm install -g yarn@latest
WORKDIR /opt/webapps/app

COPY Rakefile Gemfile Gemfile.lock ./
RUN gem install bundler && bundle install --jobs 20 --retry 5

COPY package.json ./
RUN yarn install --check-files && yarn sass-migrator division **/*.scss
# Use this for local gem troubleshooting:
# ADD vendor/gems/omniauth-tara ./vendor/gems/omniauth-tara
# ADD vendor/gems/omniauth ./vendor/gems/omniauth

RUN gem install bundler && bundle install --jobs 20 --retry 5

EXPOSE 3000
COPY package.json yarn.lock ./

CMD ["bundle", "exec", "rails", "s", "-b", "0.0.0.0"]
# RUN yarn cache clean
RUN yarn install --check-files && yarn sass-migrator division **/*.scss
16 changes: 16 additions & 0 deletions app/assets/stylesheets/components/message.scss
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,20 @@
content: '\f00c';
}
}
&--dialog {
&__alert {
background: none;
color: $orange;
&:before {
content: "\f071";
}
}
&__notice {
background: none;
color: $green;
&:before {
content: '\f00c';
}
}
}
}
12 changes: 12 additions & 0 deletions app/assets/stylesheets/components/paper.scss
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,18 @@
}
&--footer {
margin-top: 20px;
display: flex;
flex-wrap: wrap;
align-items: center;
&__left,
&__right {
display: flex;
flex-wrap: wrap;
align-items: center;
}
&__right {
margin-left: auto;
}
}
@include min-screen(767px) {
padding: 40px;
Expand Down
4 changes: 2 additions & 2 deletions app/assets/stylesheets/views/account.scss
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.page--account {
.balance_reload {
margin-top: 40px;
.paper {
margin-bottom: 40px;
}
}
2 changes: 2 additions & 0 deletions app/controllers/account_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ def show
@account = @response.account
@balance_auto_reload = @response.account[:balance_auto_reload]
@min_deposit = @response.account[:min_deposit]
@roles = @response.roles
@interfaces = @response.interfaces
end

def update
Expand Down
76 changes: 76 additions & 0 deletions app/controllers/api_users_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
class ApiUsersController < BaseController
def index
conn = ApiConnector::ApiUsers::All.new(**auth_info)
result = conn.call_action(limit: nil, offset: nil)
handle_response(result); return if performed?

@api_users = @response.users
respond_to do |format|
format.csv { format_csv }
end
end

def show
conn = ApiConnector::ApiUsers::Reader.new(**auth_info)
result = conn.call_action(id: params[:id])
handle_response(result); return if performed?

@api_user = @response.user
@roles = @response.roles
end

def create
conn = ApiConnector::ApiUsers::Creator.new(**auth_info)
result = conn.call_action(payload: api_user_payload)
handle_response(result, dialog: true); return if performed?

flash.notice = @message
redirect_to api_user_path(@response.api_user[:id])
end

def update
conn = ApiConnector::ApiUsers::Updater.new(**auth_info)
result = conn.call_action(payload: api_user_payload)
handle_response(result, dialog: true); return if performed?

flash.notice = @message
redirect_to api_user_path(@response.api_user[:id])
end

def destroy
conn = ApiConnector::ApiUsers::Deleter.new(**auth_info)
result = conn.call_action(id: params[:id])
handle_response(result); return if performed?

flash.notice = @message
redirect_to account_path
end

private

def api_user_params
params.require(:api_user).permit(:username, :password,
:identity_code, :roles, :active, :id)
end

def format_csv
raw_csv = ApiUserListCsvPresenter.new(objects: @api_users,
view: view_context).to_s
send_data raw_csv, filename: "#{filename}.csv", type: "#{Mime[:csv]}; charset=utf-8"
end

def filename
"api_users_#{l(Time.zone.now, format: :filename)}"
end

def api_user_payload
{
id: api_user_params[:id],
username: api_user_params[:username],
plain_text_password: api_user_params[:password],
identity_code: api_user_params[:identity_code],
roles: [api_user_params[:roles]],
active: api_user_params[:active] == 'true',
}
end
end
16 changes: 10 additions & 6 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def save_bulk_change_cache
end

# rubocop:disable Metrics/MethodLength
def handle_response(res)
def handle_response(res, dialog: false)
msg = res.body[:message]
if res.success
unstructable = (res[:type] == 'application/pdf' || res.body[:data].is_a?(Array))
Expand All @@ -59,14 +59,13 @@ def handle_response(res)
when 2202, 503, 401
respond_with_log_out(msg)
when 2000..2500
respond(msg)
respond(msg, dialog: dialog)
else
msg.present? ? respond(msg) : internal_server_error
msg.present? ? respond(msg, dialog: dialog) : internal_server_error
end
logger.info msg
end
end
# rubocop:enable Metrics/MethodLength

def internal_server_error
render file: 'public/500.html', layout: false,
Expand All @@ -80,18 +79,23 @@ def respond_with_log_out(msg)
end
end

def respond(msg)
def respond(msg, dialog: false)
respond_to do |format|
format.html do
flash[:alert] = msg
redirect_back_or_to "/#{I18n.locale}"
end
format.turbo_stream do
flash.now[:alert] = msg
render turbo_stream: turbo_stream.update('flash', partial: 'common/flash')
if dialog
render turbo_stream: turbo_stream.update_all('.dialog_flash', partial: 'common/dialog_flash')
else
render turbo_stream: turbo_stream.update('flash', partial: 'common/flash')
end
end
end
end
# rubocop:enable Metrics/MethodLength

def store_auth_info(token:, data:)
uuid = SecureRandom.uuid
Expand Down
70 changes: 70 additions & 0 deletions app/controllers/white_ips_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
class WhiteIpsController < BaseController
def index
conn = ApiConnector::WhiteIps::All.new(**auth_info)
result = conn.call_action(limit: nil, offset: nil)
handle_response(result); return if performed?

@white_ips = @response.ips
respond_to do |format|
format.csv { format_csv }
end
end

def show
conn = ApiConnector::WhiteIps::Reader.new(**auth_info)
result = conn.call_action(id: params[:id])
handle_response(result); return if performed?

@white_ip = @response.ip
@interfaces = @response.interfaces
end

def create
conn = ApiConnector::WhiteIps::Creator.new(**auth_info)
result = conn.call_action(payload: white_ip_payload)
handle_response(result, dialog: true); return if performed?

flash.notice = @message
redirect_to white_ip_path(@response.ip[:id])
end

def update
conn = ApiConnector::WhiteIps::Updater.new(**auth_info)
result = conn.call_action(payload: white_ip_payload)
handle_response(result, dialog: true); return if performed?

flash.notice = @message
redirect_to white_ip_path(@response.ip[:id])
end

def destroy
conn = ApiConnector::WhiteIps::Deleter.new(**auth_info)
result = conn.call_action(id: params[:id])
handle_response(result); return if performed?

flash.notice = @message
redirect_to account_path
end

private

def white_ip_params
params.require(:white_ip).permit(:id, :ipv4, :ipv6, interfaces: [])
end

def format_csv
raw_csv = WhiteIpListCsvPresenter.new(objects: @white_ips,
view: view_context).to_s
send_data raw_csv, filename: "#{filename}.csv", type: "#{Mime[:csv]}; charset=utf-8"
end

def filename
"white_ips_#{l(Time.zone.now, format: :filename)}"
end

def white_ip_payload
payload = white_ip_params
payload[:interfaces] = white_ip_params[:interfaces] || []
payload
end
end
8 changes: 8 additions & 0 deletions app/helpers/api_users_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module ApiUsersHelper
def creator(creator_str)
return unless creator_str
return 'admin' if creator_str.include?('admin') || creator_str.include?('Admin')

creator_str
end
end
6 changes: 4 additions & 2 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ def currency(amount)
format('%01.2f', amount.round(2)).sub(/\./, ',')
end

def login_with_role(user)
"#{user.username} (#{user.role}) - #{user.registrar_name}"
def login_with_role(user, registrar: true)
value = "#{user.username} (#{user.role})"
value += " - #{user.registrar_name}" if registrar
value
end

def back_link
Expand Down
Loading