Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
fortinet / lib / fortinet / localusers.rb
Size: Mime:
# frozen_string_literal: true

require 'securerandom'

module Fortinet
  # Represents a local user on the firewall
  class LocalUsers
    def initialize(username, data: nil)
      @username = username
      @data = data
    end

    attr_reader :username

    def self.all
      Fortinet.do('get', '/api/v2/cmdb/user/local')['results'].map { |data| new(data['name'], data: data) }
    end

    def load
      @data = Fortinet.do('get', "/api/v2/cmdb/user/local/#{username}")['results']&.first
    end

    def loaded?
      !@data.nil?
    end

    def self.create(username, password: nil)
      data = {}
      data['name'] = username
      data['type'] = 'password'
      data['passwd'] = password || SecureRandom.alphanumeric(16)

      response = Fortinet.do('post', '/api/v2/cmdb/user/local', data)
      response['password'] = data['passwd']
      response
    end

    def set_password(password: nil)
      data = {}
      data['passwd'] = password || SecureRandom.alphanumeric(16)

      response = Fortinet.do('put', "/api/v2/cmdb/user/local/#{@username}", data)
      response['password'] = data['passwd']
      response
    end

    def delete
      Fortinet.do('delete', "/api/v2/cmdb/user/local/#{@username}")
    rescue RestClient::InternalServerError
      raise StandardError, 'Failed to delete user (are they in a group?)'
    end
  end
end