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

remove id and name for an item

This commit is contained in:
Adrien Waksberg 2016-10-15 17:40:03 +02:00
parent 77a8a7695a
commit d5d4091bc2
3 changed files with 30 additions and 55 deletions

View file

@ -21,8 +21,6 @@ require 'i18n'
module MPW module MPW
class Item class Item
attr_accessor :id
attr_accessor :name
attr_accessor :group attr_accessor :group
attr_accessor :host attr_accessor :host
attr_accessor :protocol attr_accessor :protocol
@ -38,15 +36,13 @@ class Item
# @args: options -> a hash of parameter # @args: options -> a hash of parameter
# raise an error if the hash hasn't the key name # raise an error if the hash hasn't the key name
def initialize(options={}) def initialize(options={})
if not options.has_key?(:name) or options[:name].to_s.empty? if not options.has_key?(:host) or options[:host].to_s.empty?
raise I18n.t('error.update.name_empty') raise I18n.t('error.update.name_empty')
end end
if not options.has_key?(:id) or options[:id].to_s.empty? or not options.has_key?(:created) or options[:created].to_s.empty? if not options.has_key?(:id) or options[:id].to_s.empty? or not options.has_key?(:created) or options[:created].to_s.empty?
@id = generate_id
@created = Time.now.to_i @created = Time.now.to_i
else else
@id = options[:id]
@created = options[:created] @created = options[:created]
@last_edit = options[:last_edit] @last_edit = options[:last_edit]
options[:no_update_last_edit] = true options[:no_update_last_edit] = true
@ -58,11 +54,10 @@ class Item
# Update the item # Update the item
# @args: options -> a hash of parameter # @args: options -> a hash of parameter
def update(options={}) def update(options={})
if options.has_key?(:name) and options[:name].to_s.empty? if options.has_key?(:host) and options[:host].to_s.empty?
raise I18n.t('error.update.name_empty') raise I18n.t('error.update.name_empty')
end end
@name = options[:name] if options.has_key?(:name)
@group = options[:group] if options.has_key?(:group) @group = options[:group] if options.has_key?(:group)
@host = options[:host] if options.has_key?(:host) @host = options[:host] if options.has_key?(:host)
@protocol = options[:protocol] if options.has_key?(:protocol) @protocol = options[:protocol] if options.has_key?(:protocol)
@ -79,8 +74,6 @@ class Item
# Delete all data # Delete all data
def delete def delete
@id = nil
@name = nil
@group = nil @group = nil
@host = nil @host = nil
@protocol = nil @protocol = nil
@ -93,17 +86,11 @@ class Item
end end
def empty? def empty?
return @name.to_s.empty? return @host.to_s.empty?
end end
def nil? def nil?
return false return false
end end
# Generate an random id
private
def generate_id
return ([*('A'..'Z'),*('a'..'z'),*('0'..'9')]).sample(16).join
end
end end
end end

View file

@ -82,9 +82,7 @@ class MPW
if not data.nil? and not data.empty? if not data.nil? and not data.empty?
YAML.load(data).each_value do |d| YAML.load(data).each_value do |d|
@data.push(Item.new(id: d['id'], @data.push(Item.new(group: d['group'],
name: d['name'],
group: d['group'],
host: d['host'], host: d['host'],
protocol: d['protocol'], protocol: d['protocol'],
user: d['user'], user: d['user'],
@ -110,17 +108,15 @@ class MPW
@data.each do |item| @data.each do |item|
next if item.empty? next if item.empty?
data.merge!(item.id => {'id' => item.id, data.merge!("#{item.user}@#{item.host}" => {'group' => item.group,
'name' => item.name, 'host' => item.host,
'group' => item.group, 'protocol' => item.protocol,
'host' => item.host, 'user' => item.user,
'protocol' => item.protocol, 'port' => item.port,
'user' => item.user, 'comment' => item.comment,
'port' => item.port, 'last_edit' => item.last_edit,
'comment' => item.comment, 'created' => item.created,
'last_edit' => item.last_edit, }
'created' => item.created,
}
) )
end end
@ -251,11 +247,10 @@ class MPW
next if item.empty? next if item.empty?
next if not group.empty? and not group.eql?(item.group.downcase) next if not group.empty? and not group.eql?(item.group.downcase)
name = item.name.to_s.downcase
host = item.host.to_s.downcase host = item.host.to_s.downcase
comment = item.comment.to_s.downcase comment = item.comment.to_s.downcase
if not name =~ /^.*#{search}.*$/ and not host =~ /^.*#{search}.*$/ and not comment =~ /^.*#{search}.*$/ if not host =~ /^.*#{search}.*$/ and not comment =~ /^.*#{search}.*$/
next next
end end
@ -281,19 +276,17 @@ class MPW
def export(file) def export(file)
data = {} data = {}
@data.each do |item| @data.each do |item|
data.merge!(item.id => {'id' => item.id, data.merge!("#{item.login}@#{item.host}" => {'group' => item.group,
'name' => item.name, 'host' => item.host,
'group' => item.group, 'protocol' => item.protocol,
'host' => item.host, 'user' => item.user,
'protocol' => item.protocol, 'password' => get_password(item.id),
'user' => item.user, 'port' => item.port,
'password' => get_password(item.id), 'comment' => item.comment,
'port' => item.port, 'otp_key' => get_otp_code(item.id),
'comment' => item.comment, 'last_edit' => item.last_edit,
'otp_key' => get_otp_code(item.id), 'created' => item.created,
'last_edit' => item.last_edit, }
'created' => item.created,
}
) )
end end
@ -306,8 +299,7 @@ class MPW
# @args: file -> path to file import # @args: file -> path to file import
def import(file) def import(file)
YAML::load_file(file).each_value do |row| YAML::load_file(file).each_value do |row|
item = Item.new(name: row['name'], item = Item.new(group: row['group'],
group: row['group'],
host: row['host'], host: row['host'],
protocol: row['protocol'], protocol: row['protocol'],
user: row['user'], user: row['user'],
@ -370,8 +362,7 @@ class MPW
# Update item # Update item
if item.last_edit < r.last_edit if item.last_edit < r.last_edit
item.update(name: r.name, item.update(group: r.group,
group: r.group,
host: r.host, host: r.host,
protocol: r.protocol, protocol: r.protocol,
user: r.user, user: r.user,
@ -398,9 +389,7 @@ class MPW
remote.list.each do |r| remote.list.each do |r|
next if r.last_edit <= get_last_sync next if r.last_edit <= get_last_sync
item = Item.new(id: r.id, item = Item.new(group: r.group,
name: r.name,
group: r.group,
host: r.host, host: r.host,
protocol: r.protocol, protocol: r.protocol,
user: r.user, user: r.user,

View file

@ -1,9 +1,8 @@
--- ---
name: # <%= I18n.t('form.add_item.name') %>
group: # <%= I18n.t('form.add_item.group') %>
host: # <%= I18n.t('form.add_item.host') %> host: # <%= I18n.t('form.add_item.host') %>
protocol: # <%= I18n.t('form.add_item.protocol') %>
user: # <%= I18n.t('form.add_item.login') %> user: # <%= I18n.t('form.add_item.login') %>
group: # <%= I18n.t('form.add_item.group') %>
protocol: # <%= I18n.t('form.add_item.protocol') %>
password: # <%= I18n.t('form.add_item.password') %> password: # <%= I18n.t('form.add_item.password') %>
port: # <%= I18n.t('form.add_item.port') %> port: # <%= I18n.t('form.add_item.port') %>
comment: # <%= I18n.t('form.add_item.comment') %> comment: # <%= I18n.t('form.add_item.comment') %>