DON'T USE THIS REPO: IT'S OBSOLETE.
[versaplex.git] / versaplexd / vxsqlpool.cs
blob96cc707a049ffcc8c749957802b0e6bec676dbaf
1 /*
2 * Versaplex:
3 * Copyright (C)2007-2008 Versabanq Innovations Inc. and contributors.
4 * See the included file named LICENSE for license information.
5 */
6 using System;
7 using System.Data.Common;
8 using System.Collections;
9 using System.Collections.Generic;
10 using System.IO;
11 using Wv;
12 using Wv.Extensions;
14 public static class VxSqlPool
16 private static WvIni inifile;
18 public static void SetIniFile(string path)
20 if (path.e() || !File.Exists(path))
21 throw new Exception
22 (wv.fmt("Could not find config file '{0}'", path));
23 inifile = new WvIni(path);
26 private static string find_connection_moniker(string connid)
28 // At the moment, a connection ID is just a username
29 string dbname = inifile.get("User Map", connid);
30 if (dbname == null)
31 dbname = inifile.get("User Map", "*"); // try default
32 if (dbname == null)
33 throw new VxConfigException(
34 String.Format("No user '{0}' found.",
35 connid));
37 string cfgval = inifile.get("Connections", dbname);
38 if (cfgval == null)
39 throw new VxConfigException(String.Format(
40 "No connection found for user {0}", connid));
42 return cfgval;
45 public static string GetUsernameForCert(string cert)
47 string username = inifile.get("Cert Map", cert);
48 if (username == null)
49 throw new VxConfigException(String.Format(
50 "No user found for cert with fingerprint '{0}'.", cert));
52 return username;
55 static Dictionary<string, int> userpermissions =
56 new Dictionary<string, int>();
58 public static int access_restrictions(string connid)
60 int security_level;
61 if (!userpermissions.TryGetValue(connid, out security_level))
63 string iniseclevel = inifile.get("Security Level", connid);
65 // If we found *nothing* perhaps we should evaluate a judgement
66 // on security, see if '*' has values... assuming we're not
67 // already looking for '*''s security.
68 if (iniseclevel == null)
70 security_level = 2;
71 if (connid != "*")
73 iniseclevel = inifile.get("Security Level", "*");
75 if (iniseclevel != null &&
76 (iniseclevel == "0" ||
77 iniseclevel == "1"))
78 security_level = Convert.ToInt32(iniseclevel);
81 else
82 security_level = !(iniseclevel == "0" ||
83 iniseclevel == "1") ? 2 :
84 Convert.ToInt32(iniseclevel);
86 userpermissions[connid] = security_level;
88 return security_level;
91 public static WvDbi create(string connid)
93 try
95 return WvDbi.create(find_connection_moniker(connid));
97 catch (DbException e)
99 throw new VxConfigException("Connect: " + e.Message);