diff --git a/Notes/MileageTraker_UserEmails.xlsx b/Notes/MileageTraker_UserEmails.xlsx index 6194690..8c57f5c 100644 Binary files a/Notes/MileageTraker_UserEmails.xlsx and b/Notes/MileageTraker_UserEmails.xlsx differ diff --git a/Web.Tests/DAL/UserEmails.xls b/Web.Tests/DAL/UserEmails.xls new file mode 100644 index 0000000..6ebf6cd Binary files /dev/null and b/Web.Tests/DAL/UserEmails.xls differ diff --git a/Web.Tests/DAL/UserImporterTests.cs b/Web.Tests/DAL/UserImporterTests.cs new file mode 100644 index 0000000..b17dad2 --- /dev/null +++ b/Web.Tests/DAL/UserImporterTests.cs @@ -0,0 +1,40 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using MileageTraker.Web.DAL; +using NUnit.Framework; + +namespace Web.Tests.DAL +{ + [TestFixture] + public class UserImporterTests + { + [Test] + public void Import_Gets_More_Than_One_Result() + { + var users = UserImporter.Import("DAL\\UserEmails.xls"); + + Assert.That(users.Count(), Is.GreaterThan(0)); + } + + [Test] + public void Import_Disabled_Users() + { + var users = UserImporter.Import("DAL\\UserEmails.xls"); + var jlawson = users.Find(u => u.Username == "jlawson"); + Assert.That(jlawson.IsApproved, Is.False); + } + + [Test] + public void Import_Gets_All_Fields() + { + var users = UserImporter.Import("DAL\\UserEmails.xls"); + var dsloan = users.Find(u => u.Username == "dsloan"); + Assert.That(dsloan.Username, Is.EqualTo("dsloan")); + Assert.That(dsloan.FullName, Is.EqualTo("David Sloan")); + Assert.That(dsloan.Email, Is.EqualTo("dsloan@ethra.org")); + Assert.That(dsloan.IsApproved, Is.True); + } + } +} diff --git a/Web.Tests/Web.Tests.csproj b/Web.Tests/Web.Tests.csproj index 5049e94..52c8dbc 100644 --- a/Web.Tests/Web.Tests.csproj +++ b/Web.Tests/Web.Tests.csproj @@ -83,6 +83,7 @@ + @@ -105,6 +106,9 @@ + + PreserveNewest + diff --git a/Web/App_Data/UserEmails.xls b/Web/App_Data/UserEmails.xls new file mode 100644 index 0000000..50e2cc1 Binary files /dev/null and b/Web/App_Data/UserEmails.xls differ diff --git a/Web/DAL/UserImporter.cs b/Web/DAL/UserImporter.cs new file mode 100644 index 0000000..3774cfa --- /dev/null +++ b/Web/DAL/UserImporter.cs @@ -0,0 +1,43 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Web; +using ExcelLibrary.SpreadSheet; +using MileageTraker.Web.Models; + +namespace MileageTraker.Web.DAL +{ + public static class UserImporter + { + public static List Import(string filename) + { + var fileInfo = new FileInfo(filename); + return Import(Workbook.Load(fileInfo.FullName)).ToList(); + } + + private static IEnumerable Import(Workbook workbook) + { + var sheet = workbook.Worksheets[0]; + var first = true; + for (var r = sheet.Cells.FirstRowIndex; r <= sheet.Cells.LastRowIndex; r++) + { + var row = sheet.Cells.GetRow(r); + + if (first) + { + first = false; + continue; + } + + var user = new User(); + user.FullName = row.GetCell(0).StringValue.Trim(); + user.Username = row.GetCell(1).StringValue.Trim(); + user.Email = row.GetCell(2).StringValue.Trim(); + user.IsApproved = string.IsNullOrEmpty(row.GetCell(3).StringValue); + + yield return user; + } + } + } +} \ No newline at end of file diff --git a/Web/Migrations/201301031939259_AddLogUser.cs b/Web/Migrations/201301031939259_AddLogUser.cs index 31bace2..41ea211 100644 --- a/Web/Migrations/201301031939259_AddLogUser.cs +++ b/Web/Migrations/201301031939259_AddLogUser.cs @@ -1,11 +1,88 @@ +using System; +using System.IO; +using System.Linq; +using System.Web; +using MileageTraker.Web.DAL; +using MileageTraker.Web.Models; + namespace MileageTraker.Web.Migrations { using System.Data.Entity.Migrations; public partial class AddLogUser : DbMigration { - public override void Up() + /// + /// 0 - UserId + /// 1 - Username + /// 2 - Email + /// 3 - FullName + /// 4 - IsApproved + /// + private const string InsertUserTemplate = @"INSERT INTO [User] + ([UserId] + ,[Username] + ,[Email] + ,[FullName] + ,[Password] + ,[Comment] + ,[IsApproved] + ,[PasswordFailuresSinceLastSuccess] + ,[LastPasswordFailureDate] + ,[LastActivityDate] + ,[LastLockoutDate] + ,[LastLoginDate] + ,[Created] + ,[IsLockedOut] + ,[LastPasswordChangedDate] + ,[PasswordResetToken]) + VALUES + ('{0}' + ,'{1}' + ,'{2}' + ,'{3}' + ,'' + ,NULL + ,{4} + ,0 + ,'1753-1-1' + ,'1753-1-1' + ,'1753-1-1' + ,'1753-1-1' + ,GETDATE() + ,0 + ,'1753-1-1' + ,NULL)"; + + private const string InsertUserRoleDriverTemplate = + @"INSERT INTO [RoleUser] + ([Role_RoleId] + ,[User_UserId]) + VALUES + ('C070ADF5-CCA3-42B0-92E4-9E38472940DF' + ,'{0}')"; + + private static string GetUserInsertSql(User user) + { + return string.Format(InsertUserTemplate, + user.UserId, + user.Username, + user.Email, + user.FullName, + user.IsApproved ? 1 : 0); + } + + private static string GetUserInsertRoleDriverSql(User user) { + return string.Format(InsertUserRoleDriverTemplate, + user.UserId); + } + + public override void Up() + { + AddColumn("Log", "User_UserId", c => c.Guid()); + AddForeignKey("Log", "User_UserId", "User", "UserId"); + CreateIndex("Log", "User_UserId"); + Sql(@"INSERT INTO [Role] ([RoleId] ,[RoleName] @@ -15,19 +92,35 @@ namespace MileageTraker.Web.Migrations ,'Driver' ,'Driver')"); - - AddColumn("Log", "User_UserId", c => c.Guid()); - AddForeignKey("Log", "User_UserId", "User", "UserId"); - CreateIndex("Log", "User_UserId"); - + //var folder = AppDomain.CurrentDomain.GetData("App_Data").ToString(); + var appDomain = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "App_Data"); + var users = UserImporter.Import(Path.Combine(appDomain, "UserEmails.xls")); + users = (from u in users + where u.Username != "dsloan" && + u.Username != "ccecil" + select u).ToList(); + foreach (var user in users) + { + user.UserId = Guid.NewGuid(); + Sql(GetUserInsertSql(user)); + Sql(GetUserInsertRoleDriverSql(user)); + } } - - public override void Down() + + public override void Down() { DropIndex("Log", new[] { "User_UserId" }); DropForeignKey("Log", "User_UserId", "User"); DropColumn("Log", "User_UserId"); + Sql(@"DELETE FROM [User] + WHERE UserId IN ( + SELECT RoleUser.User_UserId + FROM RoleUser + INNER JOIN Role On RoleUser.Role_RoleId = Role.RoleId + WHERE RoleName = 'Driver' + )"); + Sql(@"DELETE FROM ROLE WHERE RoleName = 'Driver'"); } } diff --git a/Web/Web.csproj b/Web/Web.csproj index 640f8c2..bdf8486 100644 --- a/Web/Web.csproj +++ b/Web/Web.csproj @@ -125,6 +125,7 @@ + @@ -230,6 +231,9 @@ + + PreserveNewest +