class AccountsController < ApplicationController before_action :set_account, only: %i[ show edit update destroy ] before_action :set_pass, only: %i[ update ] # GET /accounts or /accounts.json def index @accounts = Account.all end # GET /accounts/1 or /accounts/1.json def show end # GET /accounts/new def new @account = Account.new end # GET /accounts/1/edit def edit end # POST /accounts or /accounts.json def create cmdline = "mkpasswd -m sha-512 -S 012345678 '#{account_params[:password]}'" crypt = `#{cmdline}`.strip @account = Account.new(account_params.except(:password).merge(encpassword: crypt)) respond_to do |format| if @account.save format.html { redirect_to account_url(@account), notice: "Account was successfully created." } format.json { render :show, status: :created, location: @account } else format.html { render :new, status: :unprocessable_entity } format.json { render json: @account.errors, status: :unprocessable_entity } end end end # PATCH/PUT /accounts/1 or /accounts/1.json def update respond_to do |format| logger.debug(account_params) if @account.update(account_params.except(:password)) format.html { redirect_to account_url(@account), notice: "Account was successfully updated." } format.json { render :show, status: :ok, location: @account } else format.html { render :edit, status: :unprocessable_entity } format.json { render json: @account.errors, status: :unprocessable_entity } end end end # DELETE /accounts/1 or /accounts/1.json def destroy @account.destroy! respond_to do |format| format.html { redirect_to accounts_url, notice: "Account was successfully destroyed." } format.json { head :no_content } end end private # Use callbacks to share common setup or constraints between actions. def set_account @account = Account.find(params[:id]) end def set_pass # [IMPORTANT] Salt must be generated for prodution ! cmdline = "mkpasswd -m sha-512 -S 012345678 '#{account_params[:password]}'" @account.encpassword = `#{cmdline}`.strip account_params.delete(:password) end # Only allow a list of trusted parameters through. def account_params params.require(:account).permit(:name, :login, :password, :sshpubkey,:encpassword) end end