Module: Zklib::UserManagement

Included in:
Zklib
Defined in:
lib/zklib/user_management.rb

Instance Method Summary collapse

Instance Method Details

#clear_adminsObject

Clear admins



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/zklib/user_management.rb', line 4

def clear_admins
  execute_cmd(
    command:        CMD_CLEAR_ADMIN,
    command_string: ''
  ) do |opts|
    return puts "ERROR: #{options[:error]}" unless opts[:valid]

    data = opts[:data]
    if data.length > 7
      data.split("\u0000").pop
    else
      puts 'ERROR: Invalid clear admins response'
    end
  end
end

#clear_usersObject

Clear users



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/zklib/user_management.rb', line 21

def clear_users
  execute_cmd(
    command:        CMD_CLEAR_DATA,
    command_string: ''
  ) do |opts|
    return puts "ERROR: #{options[:error]}" unless opts[:valid]

    data = opts[:data]
    if data.length > 7
      data.split("\u0000").pop
    else
      puts 'ERROR: Invalid clear users response'
    end
  end
end

#create_user(options) ⇒ Object

Create user param options

|_ uid       UID
|_ user_id   User ID
|_ name      Name
|_ password  Password
|_ role      Role


44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/zklib/user_management.rb', line 44

def create_user(options)
  # command_string  = (options[:uid] % 256).chr
  # command_string += (options[:uid] >> 8).chr
  command_string  = options[:uid].chr.ljust(2, 0.chr)
  command_string += options[:role].chr
  command_string += options[:password].ljust(8, 0.chr)
  command_string += options[:name].ljust(28, 0.chr)
  command_string += 1.chr
  command_string += 0.chr * 8
  command_string += options[:user_id].ljust(8, 0.chr)
  command_string += 0.chr * 16

  execute_cmd(
    command:        CMD_SET_USER,
    command_string: command_string
  ) do |opts|
    return puts "ERROR: #{options[:error]}" unless opts[:valid]

    data = opts[:data]
    if data.length > 7
      data.split("\u0000").pop
    else
      puts 'ERROR: Invalid set user response'
    end
  end
end

#decode_user_data(options) ⇒ Object

Decode user data param options

|_ data  User data to decode


74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/zklib/user_management.rb', line 74

def decode_user_data(options)
  data = options[:data]

  {
    uid:      BinData::Uint16be.read(data[0..1]).snapshot,
    role:     BinData::Uint16be.read(data[2..3]).snapshot,
    password: data[4..11].split("\0").pop,
    name:     data[12..35].split("\0").pop,
    card_no:  BinData::Uint32le.read(data[36..39]).snapshot,
    user_id:  data[49..71].split("\0").pop
  }
end

#delete_user(options) ⇒ Object

Delete user param options

|_ uid  UID


90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/zklib/user_management.rb', line 90

def delete_user(options)
  command_buffer = StringIO.new
  binary_writer  = BinData::Uint16le.new

  binary_writer.value = options[:uid]
  command_buffer.pos  = 0
  binary_writer.write(command_buffer)

  command_string = command_buffer.string

  execute_cmd(
    command:        CMD_DELETE_USER,
    command_string: command_string
  ) do |opts|
    return puts "ERROR: #{options[:error]}" unless opts[:valid]

    data = opts[:data]
    if data.length > 7
      data.split("\u0000").pop
    else
      puts 'ERROR: Invalid clear admins response'
    end
  end
end

#get_user_countObject

Get user list size



116
117
118
119
120
# File 'lib/zklib/user_management.rb', line 116

def get_user_count
  return 0 if BinData::Uint16le.read(data_recv).snapshot != CMD_PREPARE_DATA

  BinData::Uint32le.read(data_recv[8..-1]).snapshot
end

#get_usersObject

Get users



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/zklib/user_management.rb', line 123

def get_users
  header = create_header(
    command:        CMD_USERTEMP_RRQ,
    command_string: 5.chr,
    checksum:       0,
    session_id:     session_id,
    reply_id:       BinData::Uint16le.read(data_recv[6..-1]).snapshot
  )

  # Send command
  socket = UDPSocket.new
  socket.bind('0.0.0.0', inport)
  socket.send(header, 0, ip, port)

  ### START Get response size ###
  self.data_recv = receive_nonblock(socket: socket)[0]

  return puts 'ERROR: Empty response' unless data_recv && data_recv.length > 0

  self.session_id = BinData::Uint16le.read(data_recv[4..-1]).snapshot
  total_bytes     = get_user_count

  # Stop if user list is empty
  if total_bytes <= 0
    socket.close

    return []
  end
  ### END Get response size ###

  ### START Get user list ###
  bytes_recv     = 0
  rem            = nil
  offset         = 0
  user_data_size = 72
  trim_first     = 11
  trim_others    = 8
  users          = []

  while true
    data = receive_nonblock(socket: socket)[0]

    if bytes_recv == 0
      offset     = trim_first
      bytes_recv = 4
    else
      offset = trim_others
    end

    while(data.length - offset >= user_data_size)
      user_data  = data[offset..-1]
      offset    += user_data_size

      if rem && rem.length > 0
        user_data.prepend(rem)
        offset -= rem.length
        rem     = nil
      end

      users << decode_user_data(data: user_data)
      bytes_recv += user_data_size

      if bytes_recv == total_bytes
        socket.close

        return users
      end
    end

    rem = data[offset..-1]
  end
  ### END Get user list ###

  socket.close
end