1
0
Fork 0
mirror of https://github.com/nishiki/manage-password.git synced 2025-02-21 10:20:05 +00:00

add uniq id and date time

This commit is contained in:
nishiki 2014-01-13 20:07:15 +01:00
parent d458a09a75
commit bc276f23c1

View file

@ -19,6 +19,7 @@ class MPW
PASSWORD = 6 PASSWORD = 6
PORT = 7 PORT = 7
COMMENT = 8 COMMENT = 8
DATE = 9
attr_accessor :error_msg attr_accessor :error_msg
@ -40,10 +41,8 @@ class MPW
crypto = GPGME::Crypto.new(:armor => true) crypto = GPGME::Crypto.new(:armor => true)
data_decrypt = crypto.decrypt(IO.read(@file_gpg), :password => passwd).read data_decrypt = crypto.decrypt(IO.read(@file_gpg), :password => passwd).read
id = 0
data_decrypt.lines do |line| data_decrypt.lines do |line|
@data[id] = line.parse_csv.unshift(id) @data.push(line.parse_csv)
id += 1;
end end
end end
@ -63,7 +62,7 @@ class MPW
data_to_encrypt = '' data_to_encrypt = ''
@data.each do |row| @data.each do |row|
data_to_encrypt << row.drop(1).to_csv data_to_encrypt << row.to_csv
end end
crypto.encrypt(data_to_encrypt, :recipients => @key, :output => file_gpg) crypto.encrypt(data_to_encrypt, :recipients => @key, :output => file_gpg)
@ -107,13 +106,15 @@ class MPW
# @args: id -> the id item # @args: id -> the id item
# @rtrn: a row with the resultat of the search # @rtrn: a row with the resultat of the search
def searchById(id) def searchById(id)
if not @data[id.to_i].nil? @data.each do |row|
return @data[id.to_i] if @data[ID] == id
else return row
return Array.new
end end
end end
return Array.new()
end
# Add a new item # Add a new item
# @args: name -> the item name # @args: name -> the item name
# group -> the item group # group -> the item group
@ -136,16 +137,10 @@ class MPW
port = nil port = nil
end end
if not @data.last.nil? row[ID] = MPW.generatePassword(16)
id = @data.last
id = id[ID].to_i + 1
else
id = 0
end
row[ID] = id
row[PORT] = port row[PORT] = port
row[NAME] = name.force_encoding('ASCII-8BIT') row[NAME] = name.force_encoding('ASCII-8BIT')
row[DATE] = Time.now.to_i
group.nil? || group.empty? ? (row[GROUP] = nil) : (row[GROUP] = group.force_encoding('ASCII-8BIT')) group.nil? || group.empty? ? (row[GROUP] = nil) : (row[GROUP] = group.force_encoding('ASCII-8BIT'))
server.nil? || server.empty? ? (row[SERVER] = nil) : (row[SERVER] = server.force_encoding('ASCII-8BIT')) server.nil? || server.empty? ? (row[SERVER] = nil) : (row[SERVER] = server.force_encoding('ASCII-8BIT'))
protocol.nil? || protocol.empty? ? (row[PROTOCOL] = nil) : (row[PROTOCOL] = protocol.force_encoding('ASCII-8BIT')) protocol.nil? || protocol.empty? ? (row[PROTOCOL] = nil) : (row[PROTOCOL] = protocol.force_encoding('ASCII-8BIT'))
@ -153,7 +148,7 @@ class MPW
passwd.nil? || passwd.empty? ? (row[PASSWORD] = nil) : (row[PASSWORD] = passwd.force_encoding('ASCII-8BIT')) passwd.nil? || passwd.empty? ? (row[PASSWORD] = nil) : (row[PASSWORD] = passwd.force_encoding('ASCII-8BIT'))
comment.nil? || comment.empty? ? (row[COMMENT] = nil) : (row[COMMENT] = comment.force_encoding('ASCII-8BIT')) comment.nil? || comment.empty? ? (row[COMMENT] = nil) : (row[COMMENT] = comment.force_encoding('ASCII-8BIT'))
@data[id] = row @data.push(row)
return true return true
end end
@ -170,16 +165,17 @@ class MPW
# comment -> a comment # comment -> a comment
# @rtrn: true if the item has been updated # @rtrn: true if the item has been updated
def update(id, name=nil, group=nil, server=nil, protocol=nil, login=nil, passwd=nil, port=nil, comment=nil) def update(id, name=nil, group=nil, server=nil, protocol=nil, login=nil, passwd=nil, port=nil, comment=nil)
id = id.to_i i = 0
if not @data[id].nil? @data.each do |row|
if not row[ID] == id
if port.to_i <= 0 if port.to_i <= 0
port = nil port = nil
end end
row = @data[id]
row_update = Array.new() row_update = Array.new()
row[DATE] = Time.now.to_i
name.nil? || name.empty? ? (row_update[NAME] = row[NAME]) : (row_update[NAME] = name) name.nil? || name.empty? ? (row_update[NAME] = row[NAME]) : (row_update[NAME] = name)
group.nil? || group.empty? ? (row_update[GROUP] = row[GROUP]) : (row_update[GROUP] = group) group.nil? || group.empty? ? (row_update[GROUP] = row[GROUP]) : (row_update[GROUP] = group)
@ -190,26 +186,34 @@ class MPW
port.nil? || port.empty? ? (row_update[PORT] = row[PORT]) : (row_update[PORT] = port) port.nil? || port.empty? ? (row_update[PORT] = row[PORT]) : (row_update[PORT] = port)
comment.nil? || comment.empty? ? (row_update[COMMENT] = row[COMMENT]) : (row_update[COMMENT] = comment) comment.nil? || comment.empty? ? (row_update[COMMENT] = row[COMMENT]) : (row_update[COMMENT] = comment)
@data[id] = row_update @data[i] = row_update
return true return true
else end
i += 1
end
@error_msg = I18n.t('error.update.id_no_exist', :id => id) @error_msg = I18n.t('error.update.id_no_exist', :id => id)
return false return false
end end
end
# Remove an item # Remove an item
# @args: id -> the item's identifiant # @args: id -> the item's identifiant
# @rtrn: true if the item has been deleted # @rtrn: true if the item has been deleted
def remove(id) def remove(id)
if not @data.delete_at(id.to_i).nil? i = 0
@data.each do |row|
if row[ID] == id
@data.delete(i)
return true return true
else end
i += 1
end
@error_msg = I18n.t('error.delete.id_no_exist', :id => id) @error_msg = I18n.t('error.delete.id_no_exist', :id => id)
return false return false
end end
end
# Export to csv # Export to csv
# @args: file -> a string to match # @args: file -> a string to match
@ -218,7 +222,7 @@ class MPW
begin begin
File.open(file, 'w+') do |file| File.open(file, 'w+') do |file|
@data.each do |row| @data.each do |row|
row.delete_at(ID) row.delete_at(ID).delete_at(DATE)
file << row.to_csv file << row.to_csv
end end
end end