Class: HUserManager

Inherits:
Object
  • Object
show all
Includes:
BCrypt, Singleton
Defined in:
lib/husermanager/husermanager.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(databaseManager = nil) ⇒ HUserManager

Returns a new instance of HUserManager.



6
7
8
9
10
# File 'lib/husermanager/husermanager.rb', line 6

def initialize(databaseManager = nil)
  
  @databaseManager = (databaseManager == nil) ? hpgsql() : databaseManager

end

Class Method Details

.test1Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/husermanager/husermanager.rb', line 107

def self.test1()

  userProfile = {:name => "Herbert", :familienname => "Bonaffini", :adresse => "Wien", :arbeit => "Developer"}
  p hpgsql().insertValues(userProfile)
  p hpgsql().updateValues(userProfile)

  HUserManager.instance().addProfileIfNotExist(1, "familienname", "Bonaffini", 1) 
  user = {:id => 'default', :username => "[email protected]", :password => "ciao", :enabled => 1, :user_type_id => 1}
  #p "id: #{HUserManager.instance().newUser(user)}"
  p "autenticate: #{HUserManager.instance().autenticate('[email protected]', "ciao")}"
  p HUserManager.instance().activationUrl("[email protected]")
  return HUserManager.instance().getUserValueByUserName('[email protected]', 'address') 

end

Instance Method Details

#activationUrl(username) ⇒ Object



99
100
101
102
103
104
# File 'lib/husermanager/husermanager.rb', line 99

def activationUrl(username)
  
  query = "select activation from user_table where username = '#{username}'"
  return @databaseManager.run(query).dataByFieldName(0, "activation")

end

#addProfileIfNotExist(userId, fieldName, fieldValue, ordering) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/husermanager/husermanager.rb', line 48

def addProfileIfNotExist(userId, fieldName, fieldValue, ordering) 

  query = "select id from user_profile_table where profile_key = '#{fieldName}' and id = #{userId}"
  resultTable = @databaseManager.run(query).resultTable
  self.newUserProfile(userId, fieldName, fieldValue, ordering) if(resultTable.count == 0)

end

#autenticate(username, password) ⇒ Object

return nil if user not found return -1 if mismatch password return id if the username and password are ok



81
82
83
84
85
86
87
88
89
# File 'lib/husermanager/husermanager.rb', line 81

def autenticate(username, password)

  query = "select id, password from user_table where username = '#{username}'"
  resultTable = @databaseManager.run(query).resultTable
  return nil if(resultTable.count == 0)
  storePassword = resultTable[0]["password"]
  return Password.new(storePassword).is_password?(password) ? resultTable[0]["id"] : -1

end

#deleteUser(username) ⇒ Object



91
92
93
94
95
96
# File 'lib/husermanager/husermanager.rb', line 91

def deleteUser(username) 

  query = "delete from user_table where username = '#{username}'"
  @databaseManager.run(query)

end

#getUserValueById(userId, fieldName) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/husermanager/husermanager.rb', line 25

def getUserValueById(userId, fieldName)  

  return nil if(fieldName == "")
  fieldName = self.sanitize(fieldName)

  query = "select profile_value from user_profile_table where profile_key = '#{fieldName}' and user_id = '#{userId}'";
  return @databaseManager.run(query).dataByFieldName(0, "profile_value")

end

#getUserValueByUserName(userName, fieldName) ⇒ Object



36
37
38
39
40
41
42
43
44
45
# File 'lib/husermanager/husermanager.rb', line 36

def getUserValueByUserName(userName, fieldName) 

  return nil if(userName == "" || fieldName == "")

  fieldName = self.sanitize(fieldName)
  userId = userNameToUserId(userName)

  return (userId == nil) ? nil : getUserValueById(userId, fieldName)

end

#newUser(user) ⇒ Object



68
69
70
71
72
73
74
75
# File 'lib/husermanager/husermanager.rb', line 68

def newUser(user) 
 
  user[:password] = Password.create(HIO.unQuote(user[:password]))
  user[:activation] = Digest::SHA1.hexdigest([Time.now, rand].join)
  userQuery = "insert into user_table #{@databaseManager.insertValues(user)} returning id" 
  return @databaseManager.run(userQuery).dataByFieldName(0, "id")

end

#newUserProfile(userId, profileKey, profileValue, ordering) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/husermanager/husermanager.rb', line 56

def newUserProfile(userId, profileKey, profileValue, ordering) 
 
  userProfile = Hash.new()
  userProfile["user_id"] = userId
  userProfile["profile_key"] = profileKey
  userProfile["profile_value"] = profileValue
  userProfile["ordering"] = ordering
  userProfileQuery = "insert into user_profile_table #{@databaseManager.insertValues(userProfile)}"
  @databaseManager.run(userProfileQuery)

end

#sanitize(str) ⇒ Object



12
13
14
15
16
# File 'lib/husermanager/husermanager.rb', line 12

def sanitize(str)
   
  return str.gsub("'", "\'") 

end

#userNameToUserId(userName) ⇒ Object



18
19
20
21
22
23
# File 'lib/husermanager/husermanager.rb', line 18

def userNameToUserId(userName)

  query = "select id from user_table where username = '#{userName}'"
  return @databaseManager.run(query).dataByFieldName(0, "id")

end