29 using System.Collections;
30 using System.Collections.Generic;
32 using OpenSim.Framework;
33 using System.Reflection;
39 namespace OpenSim.Data.PGSQL
43 private string m_Realm;
44 private List<string> m_ColumnNames = null;
45 private int m_LastExpire = 0;
46 private string m_ConnectionString;
49 protected virtual Assembly Assembly
51 get {
return GetType().Assembly; }
57 m_ConnectionString = connectionString;
58 using (NpgsqlConnection conn =
new NpgsqlConnection(m_ConnectionString))
70 ret.Data =
new Dictionary<string, object>();
72 string sql = string.Format(
"select * from {0} where uuid = :principalID", m_Realm);
74 using (NpgsqlConnection conn =
new NpgsqlConnection(m_ConnectionString))
75 using (NpgsqlCommand cmd =
new NpgsqlCommand(sql, conn))
77 cmd.Parameters.Add(m_database.CreateParameter(
"principalID", principalID));
79 using (NpgsqlDataReader result = cmd.ExecuteReader())
83 ret.PrincipalID = principalID;
85 if (m_ColumnNames == null)
87 m_ColumnNames =
new List<string>();
89 DataTable schemaTable = result.GetSchemaTable();
90 foreach (DataRow row
in schemaTable.Rows)
91 m_ColumnNames.Add(row[
"ColumnName"].ToString());
94 foreach (
string s
in m_ColumnNames)
96 if (s ==
"UUID"||s ==
"uuid")
99 ret.Data[s] = result[s].ToString();
110 if (data.
Data.ContainsKey(
"UUID"))
111 data.
Data.Remove(
"UUID");
112 if (data.
Data.ContainsKey(
"uuid"))
113 data.
Data.Remove(
"uuid");
132 string[] fields =
new List<string>(data.Data.Keys).ToArray();
133 StringBuilder updateBuilder =
new StringBuilder();
135 using (NpgsqlConnection conn =
new NpgsqlConnection(m_ConnectionString))
136 using (NpgsqlCommand cmd =
new NpgsqlCommand())
138 updateBuilder.AppendFormat(
"update {0} set ", m_Realm);
141 foreach (
string field
in fields)
144 updateBuilder.Append(
", ");
145 updateBuilder.AppendFormat(
"\"{0}\" = :{0}",field);
149 cmd.Parameters.Add(m_database.CreateParameter(
"" + field, data.Data[field]));
152 updateBuilder.Append(
" where uuid = :principalID");
154 cmd.CommandText = updateBuilder.ToString();
155 cmd.Connection = conn;
156 cmd.Parameters.Add(m_database.CreateParameter(
"principalID", data.PrincipalID));
159 if (cmd.ExecuteNonQuery() < 1)
161 StringBuilder insertBuilder =
new StringBuilder();
163 insertBuilder.AppendFormat(
"insert into {0} (uuid, \"", m_Realm);
164 insertBuilder.Append(String.Join(
"\", \"", fields));
165 insertBuilder.Append(
"\") values (:principalID, :");
166 insertBuilder.Append(String.Join(
", :", fields));
167 insertBuilder.Append(
")");
169 cmd.CommandText = insertBuilder.ToString();
171 if (cmd.ExecuteNonQuery() < 1)
180 public bool SetDataItem(UUID principalID,
string item,
string value)
182 string sql = string.Format(
"update {0} set {1} = :{1} where uuid = :UUID", m_Realm, item);
183 using (NpgsqlConnection conn =
new NpgsqlConnection(m_ConnectionString))
184 using (NpgsqlCommand cmd =
new NpgsqlCommand(sql, conn))
186 cmd.Parameters.Add(m_database.CreateParameter(
"" + item, value));
188 if (cmd.ExecuteNonQuery() > 0)
194 public bool SetToken(UUID principalID,
string token,
int lifetime)
196 if (System.Environment.TickCount - m_LastExpire > 30000)
199 string sql =
"insert into tokens (uuid, token, validity) values (:principalID, :token, :lifetime)";
200 using (NpgsqlConnection conn =
new NpgsqlConnection(m_ConnectionString))
201 using (NpgsqlCommand cmd =
new NpgsqlCommand(sql, conn))
203 cmd.Parameters.Add(m_database.CreateParameter(
"principalID", principalID));
204 cmd.Parameters.Add(m_database.CreateParameter(
"token", token));
205 cmd.Parameters.Add(m_database.CreateParameter(
"lifetime", DateTime.Now.AddMinutes(lifetime)));
208 if (cmd.ExecuteNonQuery() > 0)
216 public bool CheckToken(UUID principalID,
string token,
int lifetime)
218 if (System.Environment.TickCount - m_LastExpire > 30000)
221 DateTime validDate = DateTime.Now.AddMinutes(lifetime);
222 string sql =
"update tokens set validity = :validDate where uuid = :principalID and token = :token and validity > (CURRENT_DATE + CURRENT_TIME)";
224 using (NpgsqlConnection conn =
new NpgsqlConnection(m_ConnectionString))
225 using (NpgsqlCommand cmd =
new NpgsqlCommand(sql, conn))
227 cmd.Parameters.Add(m_database.CreateParameter(
"principalID", principalID));
228 cmd.Parameters.Add(m_database.CreateParameter(
"token", token));
229 cmd.Parameters.Add(m_database.CreateParameter(
"validDate", validDate));
232 if (cmd.ExecuteNonQuery() > 0)
240 private void DoExpire()
242 DateTime currentDateTime = DateTime.Now;
243 string sql =
"delete from tokens where validity < :currentDateTime";
244 using (NpgsqlConnection conn =
new NpgsqlConnection(m_ConnectionString))
245 using (NpgsqlCommand cmd =
new NpgsqlCommand(sql, conn))
248 cmd.Parameters.Add(m_database.CreateParameter(
"currentDateTime", currentDateTime));
249 cmd.ExecuteNonQuery();
251 m_LastExpire = System.Environment.TickCount;
AuthenticationData Get(UUID principalID)
bool SetDataItem(UUID principalID, string item, string value)
A management class for the MS SQL Storage Engine
PGSQLAuthenticationData(string connectionString, string realm)
bool CheckToken(UUID principalID, string token, int lifetime)
Dictionary< string, object > Data
bool SetToken(UUID principalID, string token, int lifetime)
An interface for connecting to the authentication datastore
bool Store(AuthenticationData data)