mirror of
https://github.com/nishiki/manage-password.git
synced 2025-02-20 01:50:04 +00:00
fusion MPW:add and MPW:update
This commit is contained in:
parent
e8c315403e
commit
213743bad2
2 changed files with 54 additions and 83 deletions
22
lib/Cli.rb
22
lib/Cli.rb
|
@ -23,11 +23,11 @@ class Cli
|
||||||
@config = MPWConfig.new(config_file)
|
@config = MPWConfig.new(config_file)
|
||||||
|
|
||||||
if not @config.checkconfig()
|
if not @config.checkconfig()
|
||||||
self.setup(lang)
|
setup(lang)
|
||||||
end
|
end
|
||||||
|
|
||||||
@mpw = MPW.new(@config.file_gpg, @config.key)
|
@mpw = MPW.new(@config.file_gpg, @config.key)
|
||||||
if not self.decrypt()
|
if not decrypt()
|
||||||
puts "#{I18n.t('cli.display.error')}: #{@mpw.error_msg}"
|
puts "#{I18n.t('cli.display.error')}: #{@mpw.error_msg}"
|
||||||
exit 2
|
exit 2
|
||||||
end
|
end
|
||||||
|
@ -91,9 +91,9 @@ class Cli
|
||||||
if not result.empty?
|
if not result.empty?
|
||||||
result.each do |r|
|
result.each do |r|
|
||||||
if format.nil? || !format
|
if format.nil? || !format
|
||||||
self.displayFormat(r)
|
displayFormat(r)
|
||||||
else
|
else
|
||||||
self.displayFormatAlt(r)
|
displayFormatAlt(r)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
|
@ -146,7 +146,7 @@ class Cli
|
||||||
port = ask(I18n.t('cli.form.add.port')).to_s
|
port = ask(I18n.t('cli.form.add.port')).to_s
|
||||||
comment = ask(I18n.t('cli.form.add.comment')).to_s
|
comment = ask(I18n.t('cli.form.add.comment')).to_s
|
||||||
|
|
||||||
if @mpw.add(name, group, server, protocol, login, passwd, port, comment)
|
if @mpw.update(name, group, server, protocol, login, passwd, port, comment)
|
||||||
if @mpw.encrypt()
|
if @mpw.encrypt()
|
||||||
puts I18n.t('cli.form.add.valid')
|
puts I18n.t('cli.form.add.valid')
|
||||||
else
|
else
|
||||||
|
@ -174,7 +174,7 @@ class Cli
|
||||||
port = ask(I18n.t('cli.form.update.port' , :port => row[MPW::PORT])).to_s
|
port = ask(I18n.t('cli.form.update.port' , :port => row[MPW::PORT])).to_s
|
||||||
comment = ask(I18n.t('cli.form.update.comment' , :comment => row[MPW::COMMENT])).to_s
|
comment = ask(I18n.t('cli.form.update.comment' , :comment => row[MPW::COMMENT])).to_s
|
||||||
|
|
||||||
if @mpw.update(id, name, group, server, protocol, login, passwd, port, comment)
|
if @mpw.update(name, group, server, protocol, login, passwd, port, comment, id)
|
||||||
if @mpw.encrypt()
|
if @mpw.encrypt()
|
||||||
puts I18n.t('cli.form.update.valid')
|
puts I18n.t('cli.form.update.valid')
|
||||||
else
|
else
|
||||||
|
@ -196,7 +196,7 @@ class Cli
|
||||||
result = @mpw.searchById(id)
|
result = @mpw.searchById(id)
|
||||||
|
|
||||||
if result.length > 0
|
if result.length > 0
|
||||||
self.displayFormat(result)
|
displayFormat(result)
|
||||||
|
|
||||||
confirm = ask("#{I18n.t('cli.form.delete.ask', :id => id)} (y/N) ").to_s
|
confirm = ask("#{I18n.t('cli.form.delete.ask', :id => id)} (y/N) ").to_s
|
||||||
if confirm =~ /^(y|yes|YES|Yes|Y)$/
|
if confirm =~ /^(y|yes|YES|Yes|Y)$/
|
||||||
|
@ -240,7 +240,7 @@ class Cli
|
||||||
if not force
|
if not force
|
||||||
if result.is_a?(Array) && !result.empty?
|
if result.is_a?(Array) && !result.empty?
|
||||||
result.each do |r|
|
result.each do |r|
|
||||||
self.displayFormat(r)
|
displayFormat(r)
|
||||||
end
|
end
|
||||||
|
|
||||||
confirm = ask("#{I18n.t('cli.form.import.ask', :file => file)} (y/N) ").to_s
|
confirm = ask("#{I18n.t('cli.form.import.ask', :file => file)} (y/N) ").to_s
|
||||||
|
@ -286,17 +286,17 @@ class Cli
|
||||||
case command[0]
|
case command[0]
|
||||||
when 'display', 'show', 'd', 's'
|
when 'display', 'show', 'd', 's'
|
||||||
if !command[1].nil? && !command[1].empty?
|
if !command[1].nil? && !command[1].empty?
|
||||||
self.display(command[1], group, command[2])
|
display(command[1], group, command[2])
|
||||||
end
|
end
|
||||||
when 'add', 'a'
|
when 'add', 'a'
|
||||||
add()
|
add()
|
||||||
when 'update', 'u'
|
when 'update', 'u'
|
||||||
if !command[1].nil? && !command[1].empty?
|
if !command[1].nil? && !command[1].empty?
|
||||||
self.update(command[1])
|
update(command[1])
|
||||||
end
|
end
|
||||||
when 'remove', 'delete', 'r', 'd'
|
when 'remove', 'delete', 'r', 'd'
|
||||||
if !command[1].nil? && !command[1].empty?
|
if !command[1].nil? && !command[1].empty?
|
||||||
self.remove(command[1])
|
remove(command[1])
|
||||||
end
|
end
|
||||||
when 'group', 'g'
|
when 'group', 'g'
|
||||||
if !command[1].nil? && !command[1].empty?
|
if !command[1].nil? && !command[1].empty?
|
||||||
|
|
115
lib/MPW.rb
115
lib/MPW.rb
|
@ -115,44 +115,6 @@ class MPW
|
||||||
return Array.new()
|
return Array.new()
|
||||||
end
|
end
|
||||||
|
|
||||||
# Add a new item
|
|
||||||
# @args: name -> the item name
|
|
||||||
# group -> the item group
|
|
||||||
# server -> the ip or server
|
|
||||||
# protocol -> the protocol
|
|
||||||
# login -> the login
|
|
||||||
# passwd -> the password
|
|
||||||
# port -> the port
|
|
||||||
# comment -> a comment
|
|
||||||
# @rtrn: true if it works
|
|
||||||
def add(name, group=nil, server=nil, protocol=nil, login=nil, passwd=nil, port=nil, comment=nil)
|
|
||||||
row = Array.new()
|
|
||||||
|
|
||||||
if name.nil? || name.empty?
|
|
||||||
@error_msg = I18n.t('error.add.name_empty')
|
|
||||||
return false
|
|
||||||
end
|
|
||||||
|
|
||||||
if port.to_i <= 0
|
|
||||||
port = nil
|
|
||||||
end
|
|
||||||
|
|
||||||
row[ID] = MPW.generatePassword(16)
|
|
||||||
row[PORT] = port
|
|
||||||
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'))
|
|
||||||
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'))
|
|
||||||
login.nil? || login.empty? ? (row[LOGIN] = nil) : (row[LOGIN] = login.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'))
|
|
||||||
|
|
||||||
@data.push(row)
|
|
||||||
|
|
||||||
return true
|
|
||||||
end
|
|
||||||
|
|
||||||
# Update an item
|
# Update an item
|
||||||
# @args: id -> the item's identifiant
|
# @args: id -> the item's identifiant
|
||||||
# name -> the item name
|
# name -> the item name
|
||||||
|
@ -164,39 +126,49 @@ class MPW
|
||||||
# port -> the port
|
# port -> the port
|
||||||
# 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(name, group, server, protocol, login, passwd, port, comment, id=nil)
|
||||||
i = 0
|
row = Array.new()
|
||||||
|
update = false
|
||||||
|
|
||||||
@data.each do |row|
|
i = 0
|
||||||
if row[ID] == id
|
@data.each do |r|
|
||||||
|
if r[ID] == id
|
||||||
if port.to_i <= 0
|
row = r
|
||||||
port = nil
|
update = true
|
||||||
end
|
break
|
||||||
|
|
||||||
row_update = Array.new()
|
|
||||||
row_update[ID] = row[ID]
|
|
||||||
row_update[DATE] = Time.now.to_i
|
|
||||||
|
|
||||||
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)
|
|
||||||
server.nil? || server.empty? ? (row_update[SERVER] = row[SERVER]) : (row_update[SERVER] = server)
|
|
||||||
protocol.nil? || protocol.empty? ? (row_update[PROTOCOL] = row[PROTOCOL]) : (row_update[PROTOCOL] = protocol)
|
|
||||||
login.nil? || login.empty? ? (row_update[LOGIN] = row[LOGIN]) : (row_update[LOGIN] = login)
|
|
||||||
passwd.nil? || passwd.empty? ? (row_update[PASSWORD] = row[PASSWORD]) : (row_update[PASSWORD] = passwd)
|
|
||||||
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)
|
|
||||||
|
|
||||||
@data[i] = row_update
|
|
||||||
|
|
||||||
return true
|
|
||||||
end
|
end
|
||||||
|
|
||||||
i += 1
|
i += 1
|
||||||
end
|
end
|
||||||
|
|
||||||
@error_msg = I18n.t('error.update.id_no_exist', :id => id)
|
if port.to_i <= 0
|
||||||
return false
|
port = nil
|
||||||
|
end
|
||||||
|
|
||||||
|
row_update = Array.new()
|
||||||
|
row_update[DATE] = Time.now.to_i
|
||||||
|
|
||||||
|
id.nil? || id.empty? ? (row_update[ID] = MPW.generatePassword(16)) : (row_update[ID] = row[ID])
|
||||||
|
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)
|
||||||
|
server.nil? || server.empty? ? (row_update[SERVER] = row[SERVER]) : (row_update[SERVER] = server)
|
||||||
|
protocol.nil? || protocol.empty? ? (row_update[PROTOCOL] = row[PROTOCOL]) : (row_update[PROTOCOL] = protocol)
|
||||||
|
login.nil? || login.empty? ? (row_update[LOGIN] = row[LOGIN]) : (row_update[LOGIN] = login)
|
||||||
|
passwd.nil? || passwd.empty? ? (row_update[PASSWORD] = row[PASSWORD]) : (row_update[PASSWORD] = passwd)
|
||||||
|
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)
|
||||||
|
|
||||||
|
if row_update[NAME].nil? || row_update[NAME].empty?
|
||||||
|
@error_msg = I18n.t('error.update.name_empty')
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
|
||||||
|
if update
|
||||||
|
@data[i] = row_update
|
||||||
|
else
|
||||||
|
@data.push(row_update)
|
||||||
|
end
|
||||||
|
|
||||||
|
return true
|
||||||
end
|
end
|
||||||
|
|
||||||
# Remove an item
|
# Remove an item
|
||||||
|
@ -247,7 +219,7 @@ class MPW
|
||||||
return false
|
return false
|
||||||
else
|
else
|
||||||
row = line.parse_csv.unshift(0)
|
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])
|
if not update(row[NAME], row[GROUP], row[SERVER], row[PROTOCOL], row[LOGIN], row[PASSWORD], row[PORT], row[COMMENT])
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -304,7 +276,7 @@ class MPW
|
||||||
data_remote.each do |r|
|
data_remote.each do |r|
|
||||||
if l[ID] == r[ID]
|
if l[ID] == r[ID]
|
||||||
if l[DATE].to_i < r[DATE].to_i
|
if l[DATE].to_i < r[DATE].to_i
|
||||||
self.update(l[ID], r[NAME], r[GROUP], r[SERVER], r[PROTOCOL], r[LOGIN], r[PASSWORD], r[PORT], r[COMMENT])
|
update(r[NAME], r[GROUP], r[SERVER], r[PROTOCOL], r[LOGIN], r[PASSWORD], r[PORT], r[COMMENT], l[ID])
|
||||||
end
|
end
|
||||||
update = true
|
update = true
|
||||||
data_remote.delete_at(j)
|
data_remote.delete_at(j)
|
||||||
|
@ -315,19 +287,18 @@ class MPW
|
||||||
|
|
||||||
# Delete an old item
|
# Delete an old item
|
||||||
if !update && l[DATE].to_i < last_update
|
if !update && l[DATE].to_i < last_update
|
||||||
self.remove(l[ID])
|
remove(l[ID])
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Add item
|
# Add item
|
||||||
data_remote.each do |r|
|
data_remote.each do |r|
|
||||||
if r[DATE].to_i > last_update
|
if r[DATE].to_i > last_update
|
||||||
puts 'add'
|
update(r[NAME], r[GROUP], r[SERVER], r[PROTOCOL], r[LOGIN], r[PASSWORD], r[PORT], r[COMMENT], r[ID])
|
||||||
@data.push(r)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
self.encrypt()
|
encrypt()
|
||||||
|
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Reference in a new issue