1
0
Fork 0
mirror of https://github.com/nishiki/manage-password.git synced 2025-02-20 01:50:04 +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
class Item
attr_accessor :id
attr_accessor :name
attr_accessor :group
attr_accessor :host
attr_accessor :protocol
@ -38,15 +36,13 @@ class Item
# @args: options -> a hash of parameter
# raise an error if the hash hasn't the key name
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')
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?
@id = generate_id
@created = Time.now.to_i
else
@id = options[:id]
@created = options[:created]
@last_edit = options[:last_edit]
options[:no_update_last_edit] = true
@ -58,11 +54,10 @@ class Item
# Update the item
# @args: options -> a hash of parameter
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')
end
@name = options[:name] if options.has_key?(:name)
@group = options[:group] if options.has_key?(:group)
@host = options[:host] if options.has_key?(:host)
@protocol = options[:protocol] if options.has_key?(:protocol)
@ -79,8 +74,6 @@ class Item
# Delete all data
def delete
@id = nil
@name = nil
@group = nil
@host = nil
@protocol = nil
@ -93,17 +86,11 @@ class Item
end
def empty?
return @name.to_s.empty?
return @host.to_s.empty?
end
def nil?
return false
end
# Generate an random id
private
def generate_id
return ([*('A'..'Z'),*('a'..'z'),*('0'..'9')]).sample(16).join
end
end
end

View file

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