aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Data/MySQL/MySQLAuthenticationData.cs
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim/Data/MySQL/MySQLAuthenticationData.cs')
-rw-r--r--OpenSim/Data/MySQL/MySQLAuthenticationData.cs182
1 files changed, 94 insertions, 88 deletions
diff --git a/OpenSim/Data/MySQL/MySQLAuthenticationData.cs b/OpenSim/Data/MySQL/MySQLAuthenticationData.cs
index 8d82f61..7627497 100644
--- a/OpenSim/Data/MySQL/MySQLAuthenticationData.cs
+++ b/OpenSim/Data/MySQL/MySQLAuthenticationData.cs
@@ -70,99 +70,106 @@ namespace OpenSim.Data.MySQL
70 using (MySqlConnection dbcon = new MySqlConnection(m_connectionString)) 70 using (MySqlConnection dbcon = new MySqlConnection(m_connectionString))
71 { 71 {
72 dbcon.Open(); 72 dbcon.Open();
73 MySqlCommand cmd = new MySqlCommand("select * from `" + m_Realm + "` where UUID = ?principalID", dbcon);
74 cmd.Parameters.AddWithValue("?principalID", principalID.ToString());
75
76 IDataReader result = cmd.ExecuteReader();
77 73
78 if (result.Read()) 74 using (MySqlCommand cmd
75 = new MySqlCommand("select * from `" + m_Realm + "` where UUID = ?principalID", dbcon))
79 { 76 {
80 ret.PrincipalID = principalID; 77 cmd.Parameters.AddWithValue("?principalID", principalID.ToString());
81 78
82 if (m_ColumnNames == null) 79 IDataReader result = cmd.ExecuteReader();
80
81 if (result.Read())
83 { 82 {
84 m_ColumnNames = new List<string>(); 83 ret.PrincipalID = principalID;
85 84
86 DataTable schemaTable = result.GetSchemaTable(); 85 CheckColumnNames(result);
87 foreach (DataRow row in schemaTable.Rows) 86
88 m_ColumnNames.Add(row["ColumnName"].ToString()); 87 foreach (string s in m_ColumnNames)
88 {
89 if (s == "UUID")
90 continue;
91
92 ret.Data[s] = result[s].ToString();
93 }
94
95 return ret;
89 } 96 }
90 97 else
91 foreach (string s in m_ColumnNames)
92 { 98 {
93 if (s == "UUID") 99 return null;
94 continue;
95
96 ret.Data[s] = result[s].ToString();
97 } 100 }
98
99 return ret;
100 }
101 else
102 {
103 return null;
104 } 101 }
105 } 102 }
106 } 103 }
107 104
108 public bool Store(AuthenticationData data) 105 private void CheckColumnNames(IDataReader result)
109 { 106 {
110 if (data.Data.ContainsKey("UUID")) 107 if (m_ColumnNames != null)
111 data.Data.Remove("UUID"); 108 return;
112 109
113 string[] fields = new List<string>(data.Data.Keys).ToArray(); 110 List<string> columnNames = new List<string>();
114
115 MySqlCommand cmd = new MySqlCommand();
116 111
117 string update = "update `"+m_Realm+"` set "; 112 DataTable schemaTable = result.GetSchemaTable();
118 bool first = true; 113 foreach (DataRow row in schemaTable.Rows)
119 foreach (string field in fields) 114 columnNames.Add(row["ColumnName"].ToString());
120 {
121 if (!first)
122 update += ", ";
123 update += "`" + field + "` = ?"+field;
124 115
125 first = false; 116 m_ColumnNames = columnNames;
126 117 }
127 cmd.Parameters.AddWithValue("?"+field, data.Data[field]);
128 }
129 118
130 update += " where UUID = ?principalID"; 119 public bool Store(AuthenticationData data)
120 {
121 if (data.Data.ContainsKey("UUID"))
122 data.Data.Remove("UUID");
131 123
132 cmd.CommandText = update; 124 string[] fields = new List<string>(data.Data.Keys).ToArray();
133 cmd.Parameters.AddWithValue("?principalID", data.PrincipalID.ToString());
134 125
135 if (ExecuteNonQuery(cmd) < 1) 126 using (MySqlCommand cmd = new MySqlCommand())
136 { 127 {
137 string insert = "insert into `" + m_Realm + "` (`UUID`, `" + 128 string update = "update `"+m_Realm+"` set ";
138 String.Join("`, `", fields) + 129 bool first = true;
139 "`) values (?principalID, ?" + String.Join(", ?", fields) + ")"; 130 foreach (string field in fields)
140 131 {
141 cmd.CommandText = insert; 132 if (!first)
142 133 update += ", ";
134 update += "`" + field + "` = ?"+field;
135
136 first = false;
137
138 cmd.Parameters.AddWithValue("?"+field, data.Data[field]);
139 }
140
141 update += " where UUID = ?principalID";
142
143 cmd.CommandText = update;
144 cmd.Parameters.AddWithValue("?principalID", data.PrincipalID.ToString());
145
143 if (ExecuteNonQuery(cmd) < 1) 146 if (ExecuteNonQuery(cmd) < 1)
144 { 147 {
145 cmd.Dispose(); 148 string insert = "insert into `" + m_Realm + "` (`UUID`, `" +
146 return false; 149 String.Join("`, `", fields) +
150 "`) values (?principalID, ?" + String.Join(", ?", fields) + ")";
151
152 cmd.CommandText = insert;
153
154 if (ExecuteNonQuery(cmd) < 1)
155 return false;
147 } 156 }
148 } 157 }
149 158
150 cmd.Dispose();
151
152 return true; 159 return true;
153 } 160 }
154 161
155 public bool SetDataItem(UUID principalID, string item, string value) 162 public bool SetDataItem(UUID principalID, string item, string value)
156 { 163 {
157 MySqlCommand cmd = new MySqlCommand("update `" + m_Realm + 164 using (MySqlCommand cmd
158 "` set `" + item + "` = ?" + item + " where UUID = ?UUID"); 165 = new MySqlCommand("update `" + m_Realm + "` set `" + item + "` = ?" + item + " where UUID = ?UUID"))
159 166 {
160 167 cmd.Parameters.AddWithValue("?"+item, value);
161 cmd.Parameters.AddWithValue("?"+item, value); 168 cmd.Parameters.AddWithValue("?UUID", principalID.ToString());
162 cmd.Parameters.AddWithValue("?UUID", principalID.ToString()); 169
163 170 if (ExecuteNonQuery(cmd) > 0)
164 if (ExecuteNonQuery(cmd) > 0) 171 return true;
165 return true; 172 }
166 173
167 return false; 174 return false;
168 } 175 }
@@ -172,18 +179,18 @@ namespace OpenSim.Data.MySQL
172 if (System.Environment.TickCount - m_LastExpire > 30000) 179 if (System.Environment.TickCount - m_LastExpire > 30000)
173 DoExpire(); 180 DoExpire();
174 181
175 MySqlCommand cmd = new MySqlCommand("insert into tokens (UUID, token, validity) values (?principalID, ?token, date_add(now(), interval ?lifetime minute))"); 182 using (MySqlCommand cmd
176 cmd.Parameters.AddWithValue("?principalID", principalID.ToString()); 183 = new MySqlCommand(
177 cmd.Parameters.AddWithValue("?token", token); 184 "insert into tokens (UUID, token, validity) values (?principalID, ?token, date_add(now(), interval ?lifetime minute))"))
178 cmd.Parameters.AddWithValue("?lifetime", lifetime.ToString());
179
180 if (ExecuteNonQuery(cmd) > 0)
181 { 185 {
182 cmd.Dispose(); 186 cmd.Parameters.AddWithValue("?principalID", principalID.ToString());
183 return true; 187 cmd.Parameters.AddWithValue("?token", token);
188 cmd.Parameters.AddWithValue("?lifetime", lifetime.ToString());
189
190 if (ExecuteNonQuery(cmd) > 0)
191 return true;
184 } 192 }
185 193
186 cmd.Dispose();
187 return false; 194 return false;
188 } 195 }
189 196
@@ -192,30 +199,29 @@ namespace OpenSim.Data.MySQL
192 if (System.Environment.TickCount - m_LastExpire > 30000) 199 if (System.Environment.TickCount - m_LastExpire > 30000)
193 DoExpire(); 200 DoExpire();
194 201
195 MySqlCommand cmd = new MySqlCommand("update tokens set validity = date_add(now(), interval ?lifetime minute) where UUID = ?principalID and token = ?token and validity > now()"); 202 using (MySqlCommand cmd
196 cmd.Parameters.AddWithValue("?principalID", principalID.ToString()); 203 = new MySqlCommand(
197 cmd.Parameters.AddWithValue("?token", token); 204 "update tokens set validity = date_add(now(), interval ?lifetime minute) where UUID = ?principalID and token = ?token and validity > now()"))
198 cmd.Parameters.AddWithValue("?lifetime", lifetime.ToString());
199
200 if (ExecuteNonQuery(cmd) > 0)
201 { 205 {
202 cmd.Dispose(); 206 cmd.Parameters.AddWithValue("?principalID", principalID.ToString());
203 return true; 207 cmd.Parameters.AddWithValue("?token", token);
204 } 208 cmd.Parameters.AddWithValue("?lifetime", lifetime.ToString());
205 209
206 cmd.Dispose(); 210 if (ExecuteNonQuery(cmd) > 0)
211 return true;
212 }
207 213
208 return false; 214 return false;
209 } 215 }
210 216
211 private void DoExpire() 217 private void DoExpire()
212 { 218 {
213 MySqlCommand cmd = new MySqlCommand("delete from tokens where validity < now()"); 219 using (MySqlCommand cmd = new MySqlCommand("delete from tokens where validity < now()"))
214 ExecuteNonQuery(cmd); 220 {
215 221 ExecuteNonQuery(cmd);
216 cmd.Dispose(); 222 }
217 223
218 m_LastExpire = System.Environment.TickCount; 224 m_LastExpire = System.Environment.TickCount;
219 } 225 }
220 } 226 }
221} 227} \ No newline at end of file