diff options
| author | Roy Ben-Shabat <Roy@Twine-s.com> | 2018-12-24 14:46:55 +0200 |
|---|---|---|
| committer | Roy Ben-Shabat <Roy@Twine-s.com> | 2018-12-24 14:46:55 +0200 |
| commit | 0fb83fb3abb456ee6707b7f3cabc6b0c1ab2281b (patch) | |
| tree | 6b0076b6c1daacd51c2aab18aaaf15e6edb19d9e /Software/Visual_Studio/Tango.Web/SMO | |
| parent | 2f77ad3cebf771bdf02188174c9712027b004d41 (diff) | |
| download | Tango-0fb83fb3abb456ee6707b7f3cabc6b0c1ab2281b.tar.gz Tango-0fb83fb3abb456ee6707b7f3cabc6b0c1ab2281b.zip | |
Moved all common web components to Tango.Web
Changed app keys names.
Fixed issue with machine studio and the initialization of observables static collections.
Diffstat (limited to 'Software/Visual_Studio/Tango.Web/SMO')
| -rw-r--r-- | Software/Visual_Studio/Tango.Web/SMO/SmoManager.cs | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/Software/Visual_Studio/Tango.Web/SMO/SmoManager.cs b/Software/Visual_Studio/Tango.Web/SMO/SmoManager.cs new file mode 100644 index 000000000..86953d233 --- /dev/null +++ b/Software/Visual_Studio/Tango.Web/SMO/SmoManager.cs @@ -0,0 +1,73 @@ +using Microsoft.SqlServer.Management.Common; +using Microsoft.SqlServer.Management.Smo; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Web; +using Tango.Core.DB; + +namespace Tango.Web.SMO +{ + public class SmoManager : IDisposable + { + private ServerConnection _connection; + private Server _server; + private static Random random = new Random(); + + public SmoManager() + { + _connection = new ServerConnection(WebConfig.DB_ADDRESS, WebConfig.DB_USER_NAME, WebConfig.DB_PASSWORD); + _server = new Server(_connection); + } + + public DbCredentials CreateRandomLoginAndUser() + { + var database = _server.Databases.OfType<Database>().SingleOrDefault(x => x.Name == WebConfig.DB_CATALOG); + + String userName = GetRandomString(36); + String password = System.Web.Security.Membership.GeneratePassword(16, 2); + + Login login = new Login(_server, userName); + login.LoginType = LoginType.SqlLogin; + login.DefaultDatabase = WebConfig.DB_CATALOG; + login.PasswordPolicyEnforced = false; + login.Create(password); + + User user = new User(database, userName); + user.Login = userName; + user.Create(); + user.AddToRole("db_datareader"); + + return new DbCredentials() { UserName = userName, Password = password }; + } + + public void DeleteLoginAndUser(String userName) + { + var database = _server.Databases.OfType<Database>().SingleOrDefault(x => x.Name == WebConfig.DB_CATALOG); + + var user = database.Users.OfType<User>().SingleOrDefault(x => x.Name == userName); + + if (user != null) + { + user.Drop(); + } + + Login login = _server.Logins.OfType<Login>().SingleOrDefault(x => x.Name == userName); + if (login != null) + { + login.Drop(); + } + } + + public string GetRandomString(int length) + { + const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + return "TEMP_" + new string(Enumerable.Repeat(chars, length).Select(s => s[random.Next(s.Length)]).ToArray()); + } + + public void Dispose() + { + _connection.Disconnect(); + } + } +}
\ No newline at end of file |
