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