diff --git a/app/controllers/settings/employees_controller.rb b/app/controllers/settings/employees_controller.rb index 448816f5..172ccd5e 100755 --- a/app/controllers/settings/employees_controller.rb +++ b/app/controllers/settings/employees_controller.rb @@ -1,6 +1,6 @@ class Settings::EmployeesController < ApplicationController load_and_authorize_resource - before_action :set_employee, only: [:show, :edit, :update, :destroy] + before_action :set_employee, only: [:show, :edit, :update, :destroy, :change_auth_token] # GET /employees @@ -38,6 +38,13 @@ class Settings::EmployeesController < ApplicationController format.html { render :new } end end + end + + def change_auth_token + @employee.auth_token = SecureRandom.hex(10) + @employee.save + flash[:notice] = 'Auth Token was successfully updated.' + redirect_to settings_employee_url(@employee) end # PATCH/PUT /employees/1 @@ -71,6 +78,6 @@ class Settings::EmployeesController < ApplicationController # Never trust parameters from the scary internet, only allow the white list through. def employee_params - params.require(:employee).permit(:name, :role, :is_active, :emp_id, :password,:order_queue_station_id, :image_path) + params.require(:employee).permit(:name, :role, :is_active, :emp_id, :password,:order_queue_station_id, :image_path, :app_id, :auth_token) end end diff --git a/app/models/employee.rb b/app/models/employee.rb index 420491af..69b41fd2 100755 --- a/app/models/employee.rb +++ b/app/models/employee.rb @@ -1,5 +1,6 @@ class Employee < ApplicationRecord has_secure_password + # has_secure_token :auth_token has_many :commissioners has_many :shit_sales has_one :current_shift, -> { where.not(shift_started_at: nil).where(shift_closed_at: nil) },class_name: "ShiftSale" @@ -11,6 +12,7 @@ class Employee < ApplicationRecord validates :emp_id, uniqueness: true, numericality: true, length: {in: 1..4}, allow_blank: true validates :password, numericality: true, length: {in: 3..9}, allow_blank: true + before_create :generate_app_id, :generate_auth_token #, if: Proc.new { self.role == "app" } # Employee Image Uploader mount_uploader :image_path, EmployeeImageUploader @@ -89,4 +91,14 @@ class Employee < ApplicationRecord return expiry_time end + + def generate_app_id + return if self.role != 'app' + self.app_id = SecureRandom.urlsafe_base64(nil, false) + end + + def generate_auth_token + return if self.role != 'app' + self.auth_token = SecureRandom.hex(10) + end end diff --git a/app/views/settings/employees/show.html.erb b/app/views/settings/employees/show.html.erb index 87707b22..b5508f1b 100755 --- a/app/views/settings/employees/show.html.erb +++ b/app/views/settings/employees/show.html.erb @@ -54,12 +54,24 @@ <%= t("views.right_panel.detail.employee_photo") %> <%= image_tag @employee.image_path, :size => '200x200'%> + <% if @employee.role == 'app' %> + + <%= t("views.right_panel.detail.employee_app_id") %> + <%= @employee.app_id %> + + + <%= t("views.right_panel.detail.employee_auth_token") %> + <%= @employee.auth_token %> + + <% end %> <%= link_to t("views.btn.edit"), edit_settings_employee_path(@employee),:class => 'btn btn-primary btn-lg waves-effect' %> + <% if @employee.role == 'app' %> + <%= link_to t("views.btn.change_auth_token"), settings_change_auth_token_url(id: @employee.id), class: 'btn btn-primary btn-lg waves-effect' %> + <% end %> <%if @employee.role != "administrator"%> - diff --git a/config/initializers/action_controller.rb b/config/initializers/action_controller.rb index 091abd33..b0bed108 100644 --- a/config/initializers/action_controller.rb +++ b/config/initializers/action_controller.rb @@ -4,28 +4,28 @@ class ActionController::Base private def lookup_domain - if request.subdomains.last && request.subdomains.last != "www" && ENV["SERVER_CODE"] = "cloud" - from = request.host - @license = cache_license(ENV["SX_PROVISION_URL"], from) # request.subdomain.downcase - if (!@license.nil?) - logger.info "Location - " + @license.dbschema - ActiveRecord::Base.establish_connection(website_connection(@license)) - # authenticate_session_token - # logger.info "Connecting to - " + @license.subdomain + " - "+ @license.dbhost + "@" + @license.dbschema - else - # reconnect_default_db - logger.info 'License is nil' - # redirect_to root_url(:host => request.domain) + "store_error" - render :json => [{ status: false, message: 'Invalid Access!'}] - end - else - # check for license file - if check_license - current_license(ENV["SX_PROVISION_URL"]) - else - redirect_to activate_path - end - end + # if request.subdomains.last && request.subdomains.last != "www" && ENV["SERVER_CODE"] = "cloud" + # from = request.host + # @license = cache_license(ENV["SX_PROVISION_URL"], from) # request.subdomain.downcase + # if (!@license.nil?) + # logger.info "Location - " + @license.dbschema + # ActiveRecord::Base.establish_connection(website_connection(@license)) + # # authenticate_session_token + # # logger.info "Connecting to - " + @license.subdomain + " - "+ @license.dbhost + "@" + @license.dbschema + # else + # # reconnect_default_db + # logger.info 'License is nil' + # # redirect_to root_url(:host => request.domain) + "store_error" + # render :json => [{ status: false, message: 'Invalid Access!'}] + # end + # else + # # check for license file + # if check_license + # current_license(ENV["SX_PROVISION_URL"]) + # else + # redirect_to activate_path + # end + # end end diff --git a/config/locales/en.yml b/config/locales/en.yml index 36cf661c..68c7bc53 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -112,6 +112,7 @@ en: del: "DEL" clr: "CLR" assign: "ASSIGN" + change_auth_token: "Change Auth Token" print_order_summary: "Print Order Summary" memeber_card: "Member Card" @@ -678,6 +679,8 @@ en: edit_btn_txt: "to edit " delete_btn_txt: "to delete " update_btn_txt: "to update " + employee_app_id: "App ID" + employee_auth_token: "Auth Token" helpers: page_entries_info: diff --git a/config/routes.rb b/config/routes.rb index cdb0901d..0ba5e788 100755 --- a/config/routes.rb +++ b/config/routes.rb @@ -432,7 +432,9 @@ scope "(:locale)", locale: /en|mm/ do resources :display_images end #employees - resources :employees + resources :employees + get '/:id/change_auth_token' => 'employees#change_auth_token', as: 'change_auth_token' + #menu resources :menus do collection do diff --git a/db/migrate/20200114053707_add_app_id_and_auth_token_to_employees.rb b/db/migrate/20200114053707_add_app_id_and_auth_token_to_employees.rb new file mode 100644 index 00000000..a76b895e --- /dev/null +++ b/db/migrate/20200114053707_add_app_id_and_auth_token_to_employees.rb @@ -0,0 +1,9 @@ +class AddAppIdAndAuthTokenToEmployees < ActiveRecord::Migration[5.1] + def change + add_column :employees, :app_id, :string, unique: true + add_column :employees, :auth_token, :string, unique: true + + add_index :employees, :app_id + add_index :employees, :auth_token + end +end