summaryrefslogtreecommitdiff
path: root/app/models
diff options
context:
space:
mode:
Diffstat (limited to 'app/models')
-rw-r--r--app/models/admin.rb5
-rw-r--r--app/models/ip.rb5
-rw-r--r--app/models/location.rb38
-rw-r--r--app/models/nets.rb10
-rw-r--r--app/models/node.rb28
-rw-r--r--app/models/password_mailer.rb12
-rw-r--r--app/models/person.rb48
7 files changed, 146 insertions, 0 deletions
diff --git a/app/models/admin.rb b/app/models/admin.rb
new file mode 100644
index 0000000..7cd133e
--- /dev/null
+++ b/app/models/admin.rb
@@ -0,0 +1,5 @@
+class Admin < ActiveRecord::Base
+ set_table_name "admin"
+ has_many :person
+
+end
diff --git a/app/models/ip.rb b/app/models/ip.rb
new file mode 100644
index 0000000..c647078
--- /dev/null
+++ b/app/models/ip.rb
@@ -0,0 +1,5 @@
+class Ip < ActiveRecord::Base
+ set_table_name "ip"
+ belongs_to :node, :foreign_key => "node_id"
+ belongs_to :nets, :foreign_key => 'net_id'
+end
diff --git a/app/models/location.rb b/app/models/location.rb
new file mode 100644
index 0000000..5171e5d
--- /dev/null
+++ b/app/models/location.rb
@@ -0,0 +1,38 @@
+class Location < ActiveRecord::Base
+ set_table_name "location"
+ belongs_to :person
+ belongs_to :town
+ belongs_to :nets
+ has_many :node
+
+ validates_presence_of :name, :person_id
+ validates_uniqueness_of :name
+
+ validates_format_of :name,
+ :with => /^([0-9a-z]*)$/,
+ :on => :create
+ validates_format_of :name,
+ :with => /^([0-9a-z]*)$/,
+ :on => :save
+
+ @@x_zero = 4080
+ @@lon_zero = 15.43844103813
+ @@dx_dlon = 50675.5176
+ @@y_zero = 4806
+ @@lat_zero = 47.07177327969
+ @@dy_dlat = 75505.521
+
+ def lon
+ @@lon_zero + (self.pixel_x.to_f - @@x_zero) / @@dx_dlon
+ end
+
+ def lat
+ @@lat_zero + (@@y_zero - self.pixel_y.to_f) / @@dy_dlat
+ end
+
+ def comment
+ # nl2br
+ read_attribute(:comment).gsub("\n\r","<br>").gsub("\r", "").gsub("\n", "<br />")
+ end
+
+end
diff --git a/app/models/nets.rb b/app/models/nets.rb
new file mode 100644
index 0000000..59f43c6
--- /dev/null
+++ b/app/models/nets.rb
@@ -0,0 +1,10 @@
+class Nets < ActiveRecord::Base
+ set_table_name "net"
+ belongs_to :location
+ belongs_to :nettype
+ has_many :ip
+
+ validates_presence_of :netip, :netmask
+ #validates_uniqueness_of :netip
+
+end
diff --git a/app/models/node.rb b/app/models/node.rb
new file mode 100644
index 0000000..d57b6b9
--- /dev/null
+++ b/app/models/node.rb
@@ -0,0 +1,28 @@
+require "mysql"
+
+class Node < ActiveRecord::Base
+ set_table_name "node"
+ belongs_to :location
+ belongs_to :person
+ has_many :ip
+
+ validates_presence_of :name, :location_id
+ validates_uniqueness_of :name, :scope => 'location_id'
+
+ validates_format_of :name,
+ :with => /^([0-9a-z]*)$/,
+ :on => :create
+ validates_format_of :name,
+ :with => /^([0-9a-z]*)$/,
+ :on => :save
+
+
+ def comment
+ # nl2br
+ read_attribute(:comment).gsub("\n\r","<br>").gsub("\r", "").gsub("\n", "<br />")
+ end
+
+ def self.create_node(params)
+ Node.create(params)
+ end
+end
diff --git a/app/models/password_mailer.rb b/app/models/password_mailer.rb
new file mode 100644
index 0000000..bf2fe82
--- /dev/null
+++ b/app/models/password_mailer.rb
@@ -0,0 +1,12 @@
+class PasswordMailer < ActionMailer::Base
+
+ def password(person, password, sent_at = Time.now)
+ @subject = 'New Password'
+ @body = { :person => person, :password => password }
+ @recipients = person.email
+ @from = 'noreply@graz.funkfeuer.at'
+ @sent_on = sent_at
+ @headers = {}
+ end
+
+end
diff --git a/app/models/person.rb b/app/models/person.rb
new file mode 100644
index 0000000..26fa61f
--- /dev/null
+++ b/app/models/person.rb
@@ -0,0 +1,48 @@
+require "digest/md5"
+
+class Person < ActiveRecord::Base
+ set_table_name "person"
+ belongs_to :admin
+ has_many :location
+
+ attr_protected :password
+
+ validates_presence_of :email, :password, :firstname, :lastname
+ validates_uniqueness_of :email
+
+ validates_format_of :email,
+ :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i ,
+ :on => :create
+
+
+ # operator overloading for updating passwords
+ def password=(str)
+ write_attribute(:password, Digest::MD5.hexdigest(str))
+ end
+
+
+ def self.authenticate(user_name, password)
+ return Person.find( :first, :conditions =>
+ ["email = BINARY ? AND password = BINARY ?",
+ user_name,
+ Digest::MD5.hexdigest(password)] )
+ end
+
+ def self.revoke_pass(email)
+ password = newpass( 8 )
+ person = Person.find( :first,
+ :conditions => [ "email = ?", email] )
+ #
+
+ end
+
+private
+ # generate alphanumeric password
+ def newpass( len )
+ chars = ("a".."z").to_a + ("A".."Z").to_a + ("1".."9").to_a
+ newpass = ""
+ 1.upto(len) { |i| newpass << chars[rand(chars.size-1)] }
+ return newpass
+ end
+
+end