1
0
Fork 0
mirror of https://github.com/nishiki/manage-password.git synced 2024-11-27 15:43:04 +00:00

unstable: work in progress to Item class

This commit is contained in:
nishiki 2015-01-28 23:08:25 +01:00
parent 7e546b65d2
commit 16fd5d6c24
3 changed files with 79 additions and 60 deletions

View file

@ -67,6 +67,7 @@ module MPW
def set_name(name) def set_name(name)
if name.to_s.empty? if name.to_s.empty?
return false return false
end
@name = name @name = name
return true return true

View file

@ -33,8 +33,8 @@ module MPW
crypto = GPGME::Crypto.new(armor: true) crypto = GPGME::Crypto.new(armor: true)
data_decrypt = crypto.decrypt(IO.read(@file_gpg), password: password).read.force_encoding('utf-8') data_decrypt = crypto.decrypt(IO.read(@file_gpg), password: password).read.force_encoding('utf-8')
if not data_decrypt.to_s.empty? if not data_decrypt.to_s.empty?
YAML.load(data_decrypt).each do |d| YAML.load(data_decrypt).each_value do |d|
@data.push(MPW::Item.new(id: d['id'], @data.push(Item.new(id: d['id'],
name: d['name'], name: d['name'],
group: d['group'], group: d['group'],
host: d['host'], host: d['host'],
@ -46,6 +46,8 @@ module MPW
last_edit: d['last_edit'], last_edit: d['last_edit'],
created: d['created'], created: d['created'],
) )
)
end
end end
end end
@ -60,7 +62,21 @@ module MPW
def encrypt def encrypt
FileUtils.cp(@file_gpg, "#{@file_gpg}.bk") if File.exist?(@file_gpg) FileUtils.cp(@file_gpg, "#{@file_gpg}.bk") if File.exist?(@file_gpg)
data_to_encrypt = @data.to_yaml data_to_encrypt = {}
@data.each do |item|
data_to_encrypt.merge!({'id' => item.id,
'name' => item.name,
'group' => item.group,
'host' => item.host,
'protocol' => item.protocol,
'user' => item.user,
'password' => item.password,
'port' => item.port,
'comment' => item.comment,
'last_edit' => item.last_edit,
'created' => item.created,
})
end
recipients = [] recipients = []
recipients.push(@key) recipients.push(@key)
@ -70,7 +86,7 @@ module MPW
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_to_encrypt, recipients: recipients, output: file_gpg) crypto.encrypt(data_to_encrypt.to_yaml, recipients: recipients, output: file_gpg)
file_gpg.close file_gpg.close
FileUtils.rm("#{@file_gpg}.bk") if File.exist?("#{@file_gpg}.bk") FileUtils.rm("#{@file_gpg}.bk") if File.exist?("#{@file_gpg}.bk")
@ -88,7 +104,11 @@ module MPW
def list(options={}) def list(options={})
result = [] result = []
search = defined?(options[:search]) ? options[:search].downcase : '' if not defined?(options[:search]) or options[:search].nil?
search = ''
else
search = options[:search].downcase
end
@data.each do |item| @data.each do |item|
name = item.name.nil? ? nil : item.name.downcase name = item.name.nil? ? nil : item.name.downcase
@ -96,12 +116,9 @@ module MPW
comment = item.comment.nil? ? nil : item.comment.downcase comment = item.comment.nil? ? nil : item.comment.downcase
if name =~ /^.*#{search}.*$/ or host =~ /^.*#{search}.*$/ or comment =~ /^.*#{search}.*$/ if name =~ /^.*#{search}.*$/ or host =~ /^.*#{search}.*$/ or comment =~ /^.*#{search}.*$/
if (not defined?(options[:protocol] or options[:protocol].eql?(item.protocol)) and
(group.nil? or options[:group].eql?(item.group))
result.push(item) result.push(item)
end end
end end
end
return result return result
end end
@ -117,21 +134,6 @@ module MPW
return nil return nil
end end
# Remove an item
# @args: id -> the item's identifiant
# @rtrn: true if the item has been deleted
def remove(id)
@data.each_value do |row|
if row['id'] == id
@data.delete(id)
return true
end
end
@error_msg = I18n.t('error.delete.id_no_exist', id: id)
return false
end
# Export to csv # Export to csv
# @args: file -> file where you export the data # @args: file -> file where you export the data
# type -> udata type # type -> udata type
@ -140,14 +142,30 @@ module MPW
case type case type
when :csv when :csv
CSV.open(file, 'w', write_headers: true, CSV.open(file, 'w', write_headers: true,
headers: ['name', 'group', 'protocol', 'host', 'login', 'password', 'port', 'comment']) do |csv| headers: ['name', 'group', 'protocol', 'host', 'user', 'password', 'port', 'comment']) do |csv|
@data.each do |id, r| @data.each do |item|
csv << [r['name'], r['group'], r['protocol'], r['host'], r['login'], r['password'], r['port'], r['comment']] csv << [item.id, item.group, item.protocol, item.host, item.user, item.password, item.port, item.comment]
end end
end end
when :yaml when :yaml
File.open(file, 'w') {|f| f << @data.to_yaml} data = {}
@data.each do |item|
data.merge!({'id' => item.id,
'name' => item.name,
'group' => item.group,
'host' => item.host,
'protocol' => item.protocol,
'user' => item.user,
'password' => item.password,
'port' => item.port,
'comment' => item.comment,
'last_edit' => item.last_edit,
'created' => item.created,
})
end
File.open(file, 'w') {|f| f << data.to_yaml}
else else
@error_msg = "#{I18n.t('error.export.unknown_type', type: type)}" @error_msg = "#{I18n.t('error.export.unknown_type', type: type)}"

View file

@ -171,7 +171,7 @@ class Cli
# @args: search -> the string to search # @args: search -> the string to search
# protocol -> search from a particular protocol # protocol -> search from a particular protocol
def display(search, protocol=nil, group=nil) def display(search, protocol=nil, group=nil)
result = @mpw.search(search, group, protocol) result = @mpw.list(search: search)
case result.length case result.length
when 0 when 0
@ -180,10 +180,10 @@ class Cli
display_item(result.first) display_item(result.first)
else else
i = 1 i = 1
result.each do |r| result.each do |item|
print "#{i}: ".cyan print "#{i}: ".cyan
print r['name'] print item.name
print " -> #{r['comment']}".magenta if not r['comment'].to_s.empty? print " -> #{item.comment}".magenta if not item.comment.to_s.empty?
print "\n" print "\n"
i += 1 i += 1
@ -203,23 +203,23 @@ class Cli
def display_item(item) def display_item(item)
puts '--------------------'.cyan puts '--------------------'.cyan
print 'Id: '.cyan print 'Id: '.cyan
puts item['id'] puts item.id
print "#{I18n.t('display.name')}: ".cyan print "#{I18n.t('display.name')}: ".cyan
puts item['name'] puts item.name
print "#{I18n.t('display.group')}: ".cyan print "#{I18n.t('display.group')}: ".cyan
puts item['group'] puts item.group
print "#{I18n.t('display.server')}: ".cyan print "#{I18n.t('display.server')}: ".cyan
puts item['host'] puts item.host
print "#{I18n.t('display.protocol')}: ".cyan print "#{I18n.t('display.protocol')}: ".cyan
puts item['protocol'] puts item.protocol
print "#{I18n.t('display.login')}: ".cyan print "#{I18n.t('display.login')}: ".cyan
puts item['login'] puts item.user
print "#{I18n.t('display.password')}: ".cyan print "#{I18n.t('display.password')}: ".cyan
puts item['password'] puts item.password
print "#{I18n.t('display.port')}: ".cyan print "#{I18n.t('display.port')}: ".cyan
puts item['port'] puts item.port
print "#{I18n.t('display.comment')}: ".cyan print "#{I18n.t('display.comment')}: ".cyan
puts item['comment'] puts item.comment
end end
# Form to add a new item # Form to add a new item
@ -251,19 +251,19 @@ class Cli
# Update an item # Update an item
# @args: id -> the item's id # @args: id -> the item's id
def update(id) def update(id)
row = @mpw.search_by_id(id) item = @mpw.search_by_id(id)
if not row.empty? if not row.empty?
puts I18n.t('form.update.title') puts I18n.t('form.update.title')
puts '--------------------' puts '--------------------'
name = ask(I18n.t('form.update.name' , name: row['name'])).to_s name = ask(I18n.t('form.update.name' , name: item.name)).to_s
group = ask(I18n.t('form.update.group' , group: row['group'])).to_s group = ask(I18n.t('form.update.group' , group: item.group)).to_s
server = ask(I18n.t('form.update.server' , server: row['host'])).to_s server = ask(I18n.t('form.update.server' , server: item.host)).to_s
protocol = ask(I18n.t('form.update.protocol', protocol: row['protocol'])).to_s protocol = ask(I18n.t('form.update.protocol', protocol: item.protocol)).to_s
login = ask(I18n.t('form.update.login' , login: row['login'])).to_s login = ask(I18n.t('form.update.login' , login: item.user)).to_s
passwd = ask(I18n.t('form.update.password')).to_s passwd = ask(I18n.t('form.update.password')).to_s
port = ask(I18n.t('form.update.port' , port: row['port'])).to_s port = ask(I18n.t('form.update.port' , port: item.port)).to_s
comment = ask(I18n.t('form.update.comment' , comment: row['comment'])).to_s comment = ask(I18n.t('form.update.comment' , comment: item.comment)).to_s
if @mpw.update(name, group, server, protocol, login, passwd, port, comment, id) if @mpw.update(name, group, server, protocol, login, passwd, port, comment, id)
if @mpw.encrypt if @mpw.encrypt