Class: ICFS::UsersFs

Inherits:
Users
  • Object
show all
Defined in:
lib/icfs/users_fs.rb

Overview

Implements Users from a file system

Constant Summary

Constants inherited from Users

ICFS::Users::ValUser

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ UsersFs

New instance

Parameters:

  • path (String)

    Base directory



31
32
33
# File 'lib/icfs/users_fs.rb', line 31

def initialize(path)
  @path = path.dup
end

Instance Method Details

#flush(urg) ⇒ Boolean

Flush a user/role/group from a cache, if any

Parameters:

  • urg (String)

    User/Role/Group name

Returns:

  • (Boolean)

    if cached



48
# File 'lib/icfs/users_fs.rb', line 48

def flush(urg); false; end

#read(urg) ⇒ Hash

Read a user/role/group

Parameters:

  • urg (String)

    User/Role/Group name

Returns:

  • (Hash)

    Will include :type and, if a user :roles, :groups, :perms



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/icfs/users_fs.rb', line 54

def read(urg)
  Items.validate(urg, 'User/Role/Group name'.freeze, Items::FieldUsergrp)
  json = File.read(_path(urg))
  obj = Items.parse(json, 'User/Role/Group'.freeze, Users::ValUser)
  if obj['name'] != urg
    raise(Error::Values, 'UsersFs user %s name mismatch'.freeze % fn)
  end
  return obj
rescue Errno::ENOENT
  return nil
end

#write(obj) ⇒ Object

Write a user/role/group

Parameters:

  • obj (Hash)

    Will include :name, :type, and if a user :roles, :groups, :perms



70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/icfs/users_fs.rb', line 70

def write(obj)
  Items.validate(obj, 'User/Role/Group'.freeze, Users::ValUser)
  json = JSON.pretty_generate(obj)

  # write to temp file
  tmp = Tempfile.new('_tmp'.freeze, @path, :encoding => 'ascii-8bit'.freeze)
  tmp.write(json)
  tmp.close

  # move
  FileUtils.mv(tmp.path, _path(obj['name']))
  tmp.unlink
end