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

unstable: new class item

This commit is contained in:
nishiki 2015-01-27 23:17:16 +01:00
parent 271138ae80
commit 7e546b65d2
2 changed files with 113 additions and 70 deletions

80
lib/Item.rb Normal file
View file

@ -0,0 +1,80 @@
#!/usr/bin/ruby
# author: nishiki
# mail: nishiki@yaegashi.fr
# info: a simple script who manage your passwords
require 'rubygems'
require 'i18n'
module MPW
class Item
attr_accessor :error_msg
attr_accessor :id
attr_accessor :name
attr_accessor :group
attr_accessor :host
attr_accessor :protocol
attr_accessor :user
attr_accessor :password
attr_accessor :port
attr_accessor :comment
attr_accessor :last_edit
attr_accessor :created
def initialize(options={})
if not defined?(options[:id]) or not options[:id].to_s.empty? or
not defined?(options[:created]) or not options[:created].to_s.empty?
@id = generate_id
@created = Time.now.to_i
else
@id = options[:id]
@created = options[:created]
end
update(options)
end
def update(options={})
if defined?(options[:name]) and options[:name].to_s.empty?
@error_msg = I18n.t('error.update.name_empty')
return false
end
@name = options[:name] if defined?(options[:name])
@group = options[:group] if defined?(options[:group])
@host = options[:host] if defined?(options[:host])
@protocol = options[:protocol] if defined?(options[:protocol])
@user = options[:user] if defined?(options[:user])
@password = options[:password] if defined?(options[:password])
@port = options[:port].to_i if defined?(options[:port])
@comment = options[:comment] if defined?(options[:comment])
@last_edit = Time.now.to_i
return true
end
def empty?
return @name.to_s.empty?
end
def nil?
return false
end
private
def set_name(name)
if name.to_s.empty?
return false
@name = name
return true
end
private
def generate_id
return ([*('A'..'Z'),*('a'..'z'),*('0'..'9')]).sample(16).join
end
end
end

View file

@ -9,6 +9,7 @@ require 'csv'
require 'i18n'
require 'fileutils'
require 'yaml'
require "#{APP_ROOT}/lib/Item"
module MPW
class MPW
@ -21,17 +22,31 @@ module MPW
@file_gpg = file_gpg
@key = key
@share_keys = share_keys
@data = {}
@data = []
end
# Decrypt a gpg file
# @args: password -> the GPG key password
# @rtrn: true if data has been decrypted
def decrypt(passwd=nil)
def decrypt(password=nil)
if File.exist?(@file_gpg)
crypto = GPGME::Crypto.new(armor: true)
data_decrypt = crypto.decrypt(IO.read(@file_gpg), password: passwd).read.force_encoding('utf-8')
@data = YAML.load(data_decrypt) if not data_decrypt.to_s.empty?
crypto = GPGME::Crypto.new(armor: true)
data_decrypt = crypto.decrypt(IO.read(@file_gpg), password: password).read.force_encoding('utf-8')
if not data_decrypt.to_s.empty?
YAML.load(data_decrypt).each do |d|
@data.push(MPW::Item.new(id: d['id'],
name: d['name'],
group: d['group'],
host: d['host'],
protocol: d['protocol'],
user: d['login'],
password: d['password'],
port: d['port'],
comment: d['comment'],
last_edit: d['last_edit'],
created: d['created'],
)
end
end
return true
@ -70,21 +85,20 @@ module MPW
# @args: search -> the string to search
# protocol -> the connection protocol (ssh, web, other)
# @rtrn: a list with the resultat of the search
def search(search='', group=nil, protocol=nil)
def list(options={})
result = []
if not search.nil?
search = search.downcase
end
search = defined?(options[:search]) ? options[:search].downcase : ''
@data.each_value do |row|
name = row['name'].nil? ? nil : row['name'].downcase
server = row['host'].nil? ? nil : row['host'].downcase
comment = row['comment'].nil? ? nil : row['comment'].downcase
@data.each do |item|
name = item.name.nil? ? nil : item.name.downcase
host = item.host.nil? ? nil : item.host.downcase
comment = item.comment.nil? ? nil : item.comment.downcase
if name =~ /^.*#{search}.*$/ or server =~ /^.*#{search}.*$/ or comment =~ /^.*#{search}.*$/
if (protocol.nil? or protocol.eql?(row[:protocol])) and (group.nil? or group.eql?(row[:group]))
result.push(row)
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)
end
end
end
@ -96,62 +110,11 @@ module MPW
# @args: id -> the id item
# @rtrn: a row with the resultat of the search
def search_by_id(id)
@data.each_value do |row|
return row if row['id'] == id
@data.each do |item|
return item if item.id == id
end
return []
end
# Update an item
# @args: id -> the item's identifiant
# name -> the item name
# group -> the item group
# server -> the ip or hostname
# protocol -> the protocol
# login -> the login
# passwd -> the password
# port -> the port
# comment -> a comment
# @rtrn: true if the item has been updated
def update(name, group, server, protocol, login, passwd, port, comment, id=nil)
row = {}
update = false
i = 0
if @data.instance_of?(Hash) and @data.has_key?(id)
row = @data[id]
end
if port.to_i <= 0
port = nil
end
row_update = {}
row_update['id'] = id.to_s.empty? ? MPW.password(16) : id
row_update['name'] = name.to_s.empty? ? row['name'] : name
row_update['group'] = group.to_s.empty? ? row['group'] : group
row_update['host'] = server.to_s.empty? ? row['host'] : server
row_update['protocol'] = protocol.to_s.empty? ? row['protocol'] : protocol
row_update['login'] = login.to_s.empty? ? row['login'] : login
row_update['password'] = passwd.to_s.empty? ? row['password'] : passwd
row_update['port'] = port.to_s.empty? ? row['port'] : port.to_i
row_update['comment'] = comment.to_s.empty? ? row['comment'] : comment
row_update['date'] = Time.now.to_i
if row_update['name'].to_s.empty?
@error_msg = I18n.t('error.update.name_empty')
return false
end
if update
@data[id] = row_update
else
id = row_update['id']
@data[id] = row_update
end
return true
return nil
end
# Remove an item