mirror of
https://github.com/nishiki/manage-password.git
synced 2025-02-21 10:20:05 +00:00
add a temporary id
This commit is contained in:
parent
063762eb69
commit
c5ec87dc74
2 changed files with 83 additions and 89 deletions
7
Cli.rb
7
Cli.rb
|
@ -58,10 +58,9 @@ class Cli
|
||||||
result = @m.search(search, protocol)
|
result = @m.search(search, protocol)
|
||||||
|
|
||||||
if not result.empty?
|
if not result.empty?
|
||||||
i = 0
|
|
||||||
result.each do |r|
|
result.each do |r|
|
||||||
puts "# --------------------"
|
puts "# --------------------"
|
||||||
puts "# Id: #{i}"
|
puts "# Id: #{r[MPW::ID]}"
|
||||||
puts "# Name: #{r[MPW::NAME]}"
|
puts "# Name: #{r[MPW::NAME]}"
|
||||||
puts "# Group: #{r[MPW::GROUP]}"
|
puts "# Group: #{r[MPW::GROUP]}"
|
||||||
puts "# Server: #{r[MPW::SERVER]}"
|
puts "# Server: #{r[MPW::SERVER]}"
|
||||||
|
@ -70,8 +69,6 @@ class Cli
|
||||||
puts "# Password: #{r[MPW::PASSWORD]}"
|
puts "# Password: #{r[MPW::PASSWORD]}"
|
||||||
puts "# Port: #{r[MPW::PORT]}"
|
puts "# Port: #{r[MPW::PORT]}"
|
||||||
puts "# Comment: #{r[MPW::COMMENT]}"
|
puts "# Comment: #{r[MPW::COMMENT]}"
|
||||||
|
|
||||||
i += 1
|
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
puts "Nothing result!"
|
puts "Nothing result!"
|
||||||
|
@ -142,7 +139,7 @@ class Cli
|
||||||
result = @m.searchById(id)
|
result = @m.searchById(id)
|
||||||
|
|
||||||
puts "# --------------------"
|
puts "# --------------------"
|
||||||
puts "# Id: #{id}"
|
puts "# Id: #{result[MPW::ID]}"
|
||||||
puts "# Name: #{result[MPW::NAME]}"
|
puts "# Name: #{result[MPW::NAME]}"
|
||||||
puts "# Group: #{result[MPW::GROUP]}"
|
puts "# Group: #{result[MPW::GROUP]}"
|
||||||
puts "# Server: #{result[MPW::SERVER]}"
|
puts "# Server: #{result[MPW::SERVER]}"
|
||||||
|
|
165
MPW.rb
165
MPW.rb
|
@ -10,14 +10,15 @@ require 'yaml'
|
||||||
|
|
||||||
class MPW
|
class MPW
|
||||||
|
|
||||||
NAME = 0
|
ID = 0
|
||||||
GROUP = 1
|
NAME = 1
|
||||||
PROTOCOL = 2
|
GROUP = 2
|
||||||
SERVER = 3
|
PROTOCOL = 3
|
||||||
LOGIN = 4
|
SERVER = 4
|
||||||
PASSWORD = 5
|
LOGIN = 5
|
||||||
PORT = 6
|
PASSWORD = 6
|
||||||
COMMENT = 7
|
PORT = 7
|
||||||
|
COMMENT = 8
|
||||||
|
|
||||||
attr_accessor :error_msg
|
attr_accessor :error_msg
|
||||||
|
|
||||||
|
@ -93,7 +94,7 @@ class MPW
|
||||||
# @args: password -> the GPG key password
|
# @args: password -> the GPG key password
|
||||||
# @rtrn: true if data has been decrypted
|
# @rtrn: true if data has been decrypted
|
||||||
def decrypt(passwd=nil)
|
def decrypt(passwd=nil)
|
||||||
@data = ""
|
@data = Array.new
|
||||||
|
|
||||||
begin
|
begin
|
||||||
if passwd.nil? || passwd.empty?
|
if passwd.nil? || passwd.empty?
|
||||||
|
@ -112,7 +113,13 @@ class MPW
|
||||||
begin
|
begin
|
||||||
if File.exist?(@file_gpg)
|
if File.exist?(@file_gpg)
|
||||||
crypto = GPGME::Crypto.new(:armor => true)
|
crypto = GPGME::Crypto.new(:armor => true)
|
||||||
@data = 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[id] = line.parse_csv.unshift(id)
|
||||||
|
id += 1;
|
||||||
|
end
|
||||||
end
|
end
|
||||||
return true
|
return true
|
||||||
rescue
|
rescue
|
||||||
|
@ -144,7 +151,14 @@ class MPW
|
||||||
begin
|
begin
|
||||||
crypto = GPGME::Crypto.new(:armor => true)
|
crypto = GPGME::Crypto.new(:armor => true)
|
||||||
file_gpg = File.open(@file_gpg, 'w+')
|
file_gpg = File.open(@file_gpg, 'w+')
|
||||||
crypto.encrypt(@data, :recipients => @key, :output => file_gpg)
|
|
||||||
|
data_to_encrypt = ''
|
||||||
|
@data.each do |row|
|
||||||
|
row.shift
|
||||||
|
data_to_encrypt << "#{row.join(',')}\n"
|
||||||
|
end
|
||||||
|
|
||||||
|
crypto.encrypt(data_to_encrypt, :recipients => @key, :output => file_gpg)
|
||||||
file_gpg.close
|
file_gpg.close
|
||||||
|
|
||||||
return true
|
return true
|
||||||
|
@ -160,9 +174,8 @@ class MPW
|
||||||
# @rtrn: a list with the resultat of the search
|
# @rtrn: a list with the resultat of the search
|
||||||
def search(search, protocol=nil)
|
def search(search, protocol=nil)
|
||||||
result = Array.new()
|
result = Array.new()
|
||||||
@data.lines do |line|
|
@data.each do |row|
|
||||||
row = line.parse_csv
|
if row[NAME] =~ /^.*#{search}.*$/ || row[SERVER] =~ /^.*#{search}.*$/ || row[COMMENT] =~ /^.*#{search}.*$/ || protocol.eql?('all')
|
||||||
if line =~ /^.*#{search}.*$/ || protocol.eql?('all')
|
|
||||||
if protocol.nil? || protocol.eql?(row[PROTOCOL]) || protocol.eql?('all')
|
if protocol.nil? || protocol.eql?(row[PROTOCOL]) || protocol.eql?('all')
|
||||||
result.push(row)
|
result.push(row)
|
||||||
end
|
end
|
||||||
|
@ -176,16 +189,11 @@ 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)
|
||||||
i = 0
|
if not @data[id.to_i].nil?
|
||||||
@data.lines do |line|
|
return @data[id.to_i]
|
||||||
row = line.parse_csv
|
else
|
||||||
if !id.nil? && id.eql?(i.to_s)
|
return Array.new
|
||||||
return row
|
|
||||||
end
|
|
||||||
i += 1
|
|
||||||
end
|
end
|
||||||
|
|
||||||
return Array.new()
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# Add a new item
|
# Add a new item
|
||||||
|
@ -206,20 +214,28 @@ class MPW
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
if group.nil? || group.empty
|
if group.nil? || group.empty?
|
||||||
group 'No Group'
|
group = 'No Group'
|
||||||
end
|
end
|
||||||
|
|
||||||
row[NAME] = name
|
if not @data.last.nil?
|
||||||
row[GROUP] = group
|
id = @data.last
|
||||||
row[SERVER] = server
|
id = id[ID].to_i + 1
|
||||||
row[PROTOCOL] = protocol
|
else
|
||||||
row[LOGIN] = login
|
id = 0
|
||||||
row[PASSWORD] = passwd
|
end
|
||||||
row[PORT] = port
|
|
||||||
row[COMMENT] = comment
|
|
||||||
|
|
||||||
@data << "#{row.join(',')}\n"
|
row[ID] = id
|
||||||
|
row[PORT] = port
|
||||||
|
row[NAME] = name.force_encoding('ASCII-8BIT')
|
||||||
|
group.nil? ? (row[GROUP] = nil) : (row[SERVER] = server.force_encoding('ASCII-8BIT'))
|
||||||
|
server.nil? ? (row[SERVER] = nil) : (row[SERVER] = server.force_encoding('ASCII-8BIT'))
|
||||||
|
protocol.nil? ? (row[PROTOCOL] = nil) : (row[PROTOCOL] = protocol.force_encoding('ASCII-8BIT'))
|
||||||
|
login.nil? ? (row[LOGIN] = nil) : (row[LOGIN] = login.force_encoding('ASCII-8BIT'))
|
||||||
|
passwd.nil? ? (row[PASSWORD] = nil) : (row[PASSWORD] = passwd.force_encoding('ASCII-8BIT'))
|
||||||
|
comment.nil? ? (row[COMMENT] = nil) : (row[COMMENT] = comment.force_encoding('ASCII-8BIT'))
|
||||||
|
|
||||||
|
@data[id] = row
|
||||||
|
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
|
@ -236,74 +252,51 @@ 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)
|
||||||
updated = false
|
id = id.to_i
|
||||||
data_tmp = ''
|
|
||||||
|
|
||||||
i = 0
|
if not @data[id].nil?
|
||||||
@data.lines do |line|
|
row = @data[id]
|
||||||
if id.eql?(i.to_s)
|
row_update = Array.new()
|
||||||
row = line.parse_csv
|
|
||||||
row_update = Array.new()
|
|
||||||
|
|
||||||
name.empty? ? (row_update[NAME] = row[NAME]) : (row_update[NAME] = name)
|
name.empty? ? (row_update[NAME] = row[NAME]) : (row_update[NAME] = name)
|
||||||
group.empty? ? (row_update[GROUP] = row[GROUP]) : (row_update[GROUP] = group)
|
group.empty? ? (row_update[GROUP] = row[GROUP]) : (row_update[GROUP] = group)
|
||||||
server.empty? ? (row_update[SERVER] = row[SERVER]) : (row_update[SERVER] = server)
|
server.empty? ? (row_update[SERVER] = row[SERVER]) : (row_update[SERVER] = server)
|
||||||
protocol.empty? ? (row_update[PROTOCOL] = row[PROTOCOL]) : (row_update[PROTOCOL] = protocol)
|
protocol.empty? ? (row_update[PROTOCOL] = row[PROTOCOL]) : (row_update[PROTOCOL] = protocol)
|
||||||
login.empty? ? (row_update[LOGIN] = row[LOGIN]) : (row_update[LOGIN] = login)
|
login.empty? ? (row_update[LOGIN] = row[LOGIN]) : (row_update[LOGIN] = login)
|
||||||
passwd.empty? ? (row_update[PASSWORD] = row[PASSWORD]) : (row_update[PASSWORD] = passwd)
|
passwd.empty? ? (row_update[PASSWORD] = row[PASSWORD]) : (row_update[PASSWORD] = passwd)
|
||||||
port.empty? ? (row_update[PORT] = row[PORT]) : (row_update[PORT] = port)
|
port.empty? ? (row_update[PORT] = row[PORT]) : (row_update[PORT] = port)
|
||||||
comment.empty? ? (row_update[COMMENT] = row[COMMENT]) : (row_update[COMMENT] = comment)
|
comment.empty? ? (row_update[COMMENT] = row[COMMENT]) : (row_update[COMMENT] = comment)
|
||||||
|
|
||||||
data_tmp << "#{row_update.join(',')}\n"
|
@data[id] = row_update
|
||||||
updated = true
|
|
||||||
else
|
|
||||||
data_tmp << line
|
|
||||||
end
|
|
||||||
|
|
||||||
i += 1
|
return true
|
||||||
|
else
|
||||||
|
@error_msg = "Can't update the item, the item #{id} doesn't exist!"
|
||||||
|
return false
|
||||||
end
|
end
|
||||||
@data = data_tmp
|
|
||||||
|
|
||||||
if not updated
|
|
||||||
@error_msg = "Can't update the item: #{id}!"
|
|
||||||
end
|
|
||||||
|
|
||||||
return updated
|
|
||||||
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)
|
||||||
removed = false
|
if not @data.delete_at(id.to_i).nil?
|
||||||
data_tmp = ""
|
return true
|
||||||
|
else
|
||||||
i = 0
|
return false
|
||||||
@data.lines do |line|
|
|
||||||
if id.eql?(i.to_s)
|
|
||||||
removed = true
|
|
||||||
else
|
|
||||||
data_tmp << line
|
|
||||||
end
|
|
||||||
i += 1
|
|
||||||
end
|
end
|
||||||
@data = data_tmp
|
|
||||||
|
|
||||||
if not removed
|
|
||||||
@error_msg = "Can't remove the item: #{id}!"
|
|
||||||
end
|
|
||||||
|
|
||||||
return removed
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
# Export to csv
|
# Export to csv
|
||||||
# @args: file -> a string to match
|
# @args: file -> a string to match
|
||||||
# @rtrn: true if export work
|
# @rtrn: true if export work
|
||||||
def export(file)
|
def export(file)
|
||||||
begin
|
begin
|
||||||
File.open(file, 'w+') do |f|
|
File.open(file, 'w+') do |file|
|
||||||
f << @data
|
@data.each do |row|
|
||||||
|
row.delete_at(ID)
|
||||||
|
file << "#{row.join(',')}\n"
|
||||||
|
end
|
||||||
end
|
end
|
||||||
return true
|
return true
|
||||||
rescue
|
rescue
|
||||||
|
@ -322,9 +315,13 @@ class MPW
|
||||||
if not line =~ /(.*,){6}/
|
if not line =~ /(.*,){6}/
|
||||||
@error_msg = "Can't import, the file is bad format!"
|
@error_msg = "Can't import, the file is bad format!"
|
||||||
return false
|
return false
|
||||||
|
else
|
||||||
|
row = line.parse_csv.unshift(0)
|
||||||
|
if not add(row[NAME], row[GROUP], row[SERVER], row[PROTOCOL], row[LOGIN], row[PASSWORD], row[PORT], row[COMMENT])
|
||||||
|
return false
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@data << data_new.force_encoding("ASCII-8BIT")
|
|
||||||
|
|
||||||
return true
|
return true
|
||||||
rescue
|
rescue
|
||||||
|
|
Loading…
Add table
Reference in a new issue