diff --git a/Teknik/Areas/Blog/Controllers/BlogController.cs b/Teknik/Areas/Blog/Controllers/BlogController.cs index d7a23ed..5fe596b 100644 --- a/Teknik/Areas/Blog/Controllers/BlogController.cs +++ b/Teknik/Areas/Blog/Controllers/BlogController.cs @@ -31,7 +31,7 @@ namespace Teknik.Areas.Blog.Controllers bool isAuth = User.IsInRole("Admin"); var foundPosts = db.BlogPosts.Include("Blog").Include("Blog.User").Where(p => ((p.System || isAuth) && p.Published)); model = new BlogViewModel(); - model.BlogId = Constants.SERVERBLOGID; + model.BlogId = Config.BlogConfig.ServerBlogId; User user = (User.IsInRole("Admin")) ? db.Users.Where(u => u.Username == User.Identity.Name).First() : null; model.UserId = (user != null) ? user.UserId : 0; @@ -44,7 +44,7 @@ namespace Teknik.Areas.Blog.Controllers } else // A user specific blog { - Models.Blog blog = db.Blogs.Include("User").Where(p => p.User.Username == username && p.BlogId != Constants.SERVERBLOGID).FirstOrDefault(); + Models.Blog blog = db.Blogs.Include("User").Where(p => p.User.Username == username && p.BlogId != Config.BlogConfig.ServerBlogId).FirstOrDefault(); // find the blog specified if (blog != null) { @@ -94,7 +94,7 @@ namespace Teknik.Areas.Blog.Controllers public ActionResult GetPosts(int blogID, int startPostID, int count) { bool isAuth = User.IsInRole("Admin"); - var posts = db.BlogPosts.Include("Blog").Include("Blog.User").Where(p => ((p.BlogId == blogID && !p.System) || (p.System && blogID == Constants.SERVERBLOGID)) && + var posts = db.BlogPosts.Include("Blog").Include("Blog.User").Where(p => ((p.BlogId == blogID && !p.System) || (p.System && blogID == Config.BlogConfig.ServerBlogId)) && (p.Published || p.Blog.User.Username == User.Identity.Name || isAuth)).OrderByDescending(p => p.DatePosted).Skip(startPostID).Take(count).ToList(); List postViews = new List(); if (posts != null) @@ -141,7 +141,7 @@ namespace Teknik.Areas.Blog.Controllers { if (User.IsInRole("Admin") || db.Blogs.Where(b => b.User.Username == User.Identity.Name).FirstOrDefault() != null) { - bool system = (blogID == Constants.SERVERBLOGID); + bool system = (blogID == Config.BlogConfig.ServerBlogId); if (system) { var user = db.Blogs.Include("User").Where(b => b.User.Username == User.Identity.Name); diff --git a/Teknik/Areas/Profile/Controllers/ProfileController.cs b/Teknik/Areas/Profile/Controllers/ProfileController.cs index b85eef3..01e9715 100644 --- a/Teknik/Areas/Profile/Controllers/ProfileController.cs +++ b/Teknik/Areas/Profile/Controllers/ProfileController.cs @@ -227,7 +227,7 @@ namespace Teknik.Areas.Profile.Controllers string json = Newtonsoft.Json.JsonConvert.SerializeObject(obj); client.Headers[HttpRequestHeader.ContentType] = "application/json"; Uri baseUri = new Uri(Config.GitConfig.Host); - Uri finalUri = new Uri(baseUri, "admin/users?token=" + Config.GitConfig.AccessToken); + Uri finalUri = new Uri(baseUri, "api/v1/admin/users?token=" + Config.GitConfig.AccessToken); string result = client.UploadString(finalUri, "POST", json); } } @@ -245,7 +245,7 @@ namespace Teknik.Areas.Profile.Controllers // Generate blog for the user var newBlog = db.Blogs.Create(); - newBlog.UserId = db.Users.Where(u => u.Username == model.Username).Select(u => u.UserId).First(); + newBlog.UserId = newUser.UserId; db.Blogs.Add(newBlog); db.SaveChanges(); } @@ -306,7 +306,7 @@ namespace Teknik.Areas.Profile.Controllers string json = Newtonsoft.Json.JsonConvert.SerializeObject(obj); client.Headers[HttpRequestHeader.ContentType] = "application/json"; Uri baseUri = new Uri(Config.GitConfig.Host); - Uri finalUri = new Uri(baseUri, "admin/users/" + User.Identity.Name + "?token=" + Config.GitConfig.AccessToken); + Uri finalUri = new Uri(baseUri, "api/v1/admin/users/" + User.Identity.Name + "?token=" + Config.GitConfig.AccessToken); string result = client.UploadString(finalUri, "PATCH", json); } } @@ -349,7 +349,7 @@ namespace Teknik.Areas.Profile.Controllers if (Config.GitConfig.Enabled) { Uri baseUri = new Uri(Config.GitConfig.Host); - Uri finalUri = new Uri(baseUri, "admin/users/" + User.Identity.Name + "?token=" + Config.GitConfig.AccessToken); + Uri finalUri = new Uri(baseUri, "api/v1/admin/users/" + User.Identity.Name + "?token=" + Config.GitConfig.AccessToken); WebRequest request = WebRequest.Create(finalUri); request.Method = "DELETE"; diff --git a/Teknik/Areas/RSS/Controllers/RSSController.cs b/Teknik/Areas/RSS/Controllers/RSSController.cs index 84db844..4131784 100644 --- a/Teknik/Areas/RSS/Controllers/RSSController.cs +++ b/Teknik/Areas/RSS/Controllers/RSSController.cs @@ -36,7 +36,7 @@ namespace Teknik.Areas.RSS.Controllers bool isSystem = string.IsNullOrEmpty(username); if (isSystem) { - blog = db.Blogs.Include("BlogPosts").Include("User").Where(b => b.BlogId == Constants.SERVERBLOGID).FirstOrDefault(); + blog = db.Blogs.Include("BlogPosts").Include("User").Where(b => b.BlogId == Config.BlogConfig.ServerBlogId).FirstOrDefault(); blogUrl = Url.SubRouteUrl("blog", "Blog.Blog"); } else diff --git a/Teknik/Configuration/BlogConfig.cs b/Teknik/Configuration/BlogConfig.cs index cb3242a..d4c2435 100644 --- a/Teknik/Configuration/BlogConfig.cs +++ b/Teknik/Configuration/BlogConfig.cs @@ -12,6 +12,7 @@ namespace Teknik.Configuration public string Description { get; set; } public int PostsToLoad { get; set; } public int CommentsToLoad { get; set; } + public int ServerBlogId { get; set; } public BlogConfig() { @@ -25,6 +26,7 @@ namespace Teknik.Configuration Description = string.Empty; PostsToLoad = 10; CommentsToLoad = 10; + ServerBlogId = 1; } } } \ No newline at end of file diff --git a/Teknik/Helpers/Constants.cs b/Teknik/Helpers/Constants.cs index b411ba1..88b4106 100644 --- a/Teknik/Helpers/Constants.cs +++ b/Teknik/Helpers/Constants.cs @@ -8,9 +8,7 @@ namespace Teknik.Helpers { public static class Constants { - // Blog Constants - public static int SERVERBLOGID = 0; - + public const string SERVERUSER = "Server Admin"; // Paste Constants public static Dictionary HIGHLIGHTFORMATS = new Dictionary() { diff --git a/Teknik/Helpers/MysqlDatabase.cs b/Teknik/Helpers/MysqlDatabase.cs index 85bc77a..81af722 100644 --- a/Teknik/Helpers/MysqlDatabase.cs +++ b/Teknik/Helpers/MysqlDatabase.cs @@ -128,7 +128,7 @@ namespace Teknik.Helpers { if (config.Server != string.Empty && config.Database != string.Empty && config.Username != string.Empty && config.Password != string.Empty) { - string strCon = string.Format("Server={0}; database={1}; user={2}; password={3}; port={4}; charset=utf8", config.Server, config.Database, config.Username, config.Password, config.Port); + string strCon = string.Format("Server={0}; database={1}; user={2}; password={3}; port={4}; charset=utf8; Allow Zero Datetime=true;", config.Server, config.Database, config.Username, config.Password, config.Port); Connection = new MySqlConnection(strCon); try { diff --git a/Teknik/Migrations/Configuration.cs b/Teknik/Migrations/Configuration.cs index 8d8518f..faa8388 100644 --- a/Teknik/Migrations/Configuration.cs +++ b/Teknik/Migrations/Configuration.cs @@ -22,43 +22,64 @@ namespace Teknik.Migrations Config config = Config.Load(); // Pre-populate with the default stuff - // Create server blog - Areas.Blog.Models.Blog serverBlog = new Areas.Blog.Models.Blog(); - context.Blogs.Add(serverBlog); + // Create system blog + Areas.Profile.Models.User systemUser = new Areas.Profile.Models.User(); + systemUser.Username = Constants.SERVERUSER; + systemUser.JoinDate = DateTime.Now; + systemUser.LastSeen = DateTime.Now; + systemUser.UserSettings = new Areas.Profile.Models.UserSettings(); + systemUser.BlogSettings = new Areas.Profile.Models.BlogSettings(); + systemUser.UploadSettings = new Areas.Profile.Models.UploadSettings(); + context.Users.AddOrUpdate(systemUser); + context.SaveChanges(); + + Areas.Blog.Models.Blog systemBlog = new Areas.Blog.Models.Blog(); + systemBlog.UserId = systemUser.UserId; + systemBlog.BlogId = config.BlogConfig.ServerBlogId; + context.Blogs.AddOrUpdate(systemBlog); context.SaveChanges(); // Create roles and groups Areas.Profile.Models.Role adminRole = new Areas.Profile.Models.Role(); adminRole.Name = "Admin"; adminRole.Description = "Allows complete access to user specific actions"; - context.Roles.Add(adminRole); + context.Roles.AddOrUpdate(adminRole); Areas.Profile.Models.Role podcastRole = new Areas.Profile.Models.Role(); podcastRole.Name = "Podcast"; podcastRole.Description = "Allows create/edit/delete access to podcasts"; - context.Roles.Add(podcastRole); + context.Roles.AddOrUpdate(podcastRole); Areas.Profile.Models.Group adminGroup = new Areas.Profile.Models.Group(); adminGroup.Name = "Administrators"; adminGroup.Description = "System Administrators with full access"; + adminGroup.Roles = new List(); adminGroup.Roles.Add(adminRole); adminGroup.Roles.Add(podcastRole); + context.Groups.AddOrUpdate(adminGroup); Areas.Profile.Models.Group podcastGroup = new Areas.Profile.Models.Group(); podcastGroup.Name = "Podcast"; podcastGroup.Description = "Podcast team members"; + podcastGroup.Roles = new List(); podcastGroup.Roles.Add(podcastRole); + context.Groups.AddOrUpdate(podcastGroup); + + Areas.Profile.Models.Group memberGroup = new Areas.Profile.Models.Group(); + memberGroup.Name = "Member"; + memberGroup.Description = "The default member group with basic permissions"; + context.Groups.AddOrUpdate(memberGroup); context.SaveChanges(); - if (config.DatabaseConfig.Migrate && !config.DevEnvironment) { // Convert legacy MySQL DB to new MS SQL DB MysqlDatabase db = new MysqlDatabase(config.DatabaseConfig); + db.MysqlErrorEvent += Db_MysqlErrorEvent; // Transfer transactions - var transRet = db.Query("SELECT * FROM transactions"); + var transRet = db.Query("SELECT * FROM `transactions`"); foreach (var tran in transRet) { switch (tran["trans_type"].ToString()) @@ -66,29 +87,29 @@ namespace Teknik.Migrations case "One-Time": Areas.Transparency.Models.OneTime tr = new Areas.Transparency.Models.OneTime(); tr.DateSent = DateTime.Parse(tran["date_posted"].ToString()); - tr.Amount = Int32.Parse(tran["amount"].ToString()); + tr.Amount = Double.Parse(tran["amount"].ToString()); tr.Currency = tran["currency"].ToString(); tr.Recipient = tran["recipient"].ToString(); tr.Reason = tran["reason"].ToString(); - context.Transactions.Add(tr); + context.Transactions.AddOrUpdate(tr); break; case "Bill": Areas.Transparency.Models.Bill bill = new Areas.Transparency.Models.Bill(); bill.DateSent = DateTime.Parse(tran["date_posted"].ToString()); - bill.Amount = Int32.Parse(tran["amount"].ToString()); + bill.Amount = Double.Parse(tran["amount"].ToString()); bill.Currency = tran["currency"].ToString(); bill.Recipient = tran["recipient"].ToString(); bill.Reason = tran["reason"].ToString(); - context.Transactions.Add(bill); + context.Transactions.AddOrUpdate(bill); break; case "Donation": Areas.Transparency.Models.Donation don = new Areas.Transparency.Models.Donation(); don.DateSent = DateTime.Parse(tran["date_posted"].ToString()); - don.Amount = Int32.Parse(tran["amount"].ToString()); + don.Amount = Double.Parse(tran["amount"].ToString()); don.Currency = tran["currency"].ToString(); don.Sender = tran["sender"].ToString(); don.Reason = tran["reason"].ToString(); - context.Transactions.Add(don); + context.Transactions.AddOrUpdate(don); break; } } @@ -97,7 +118,7 @@ namespace Teknik.Migrations // Transfer Users and Blogs/Posts Dictionary userMapping = new Dictionary(); Dictionary postMapping = new Dictionary(); - var userRet = db.Query("SELECT * FROM users"); + var userRet = db.Query("SELECT * FROM `users`"); foreach (var user in userRet) { // Create User @@ -109,26 +130,36 @@ namespace Teknik.Migrations newUser.Username = user["username"].ToString(); newUser.HashedPassword = user["password"].ToString(); newUser.JoinDate = DateTime.Parse(user["join_date"].ToString()); - newUser.LastSeen = DateTime.Parse(user["last_seen"].ToString()); + newUser.LastSeen = DateTime.Parse(user["join_date"].ToString()); newUser.UserSettings.About = user["about"].ToString(); newUser.UserSettings.Website = user["website"].ToString(); newUser.UserSettings.Quote = user["quote"].ToString(); newUser.BlogSettings.Title = user["blog_title"].ToString(); newUser.BlogSettings.Description = user["blog_desc"].ToString(); - context.Users.Add(newUser); + if (user["site_admin"].ToString() == "1") + { + newUser.Groups.Add(adminGroup); + } + else + { + newUser.Groups.Add(memberGroup); + } + context.Users.AddOrUpdate(newUser); context.SaveChanges(); + int oldUserId = Int32.Parse(user["id"].ToString()); int userId = newUser.UserId; - userMapping.Add(Int32.Parse(user["id"].ToString()), userId); + userMapping.Add(oldUserId, userId); // Create Blog for user Areas.Blog.Models.Blog newBlog = new Areas.Blog.Models.Blog(); newBlog.UserId = userId; + context.Blogs.AddOrUpdate(newBlog); context.SaveChanges(); int blogId = newBlog.BlogId; // Transfer Blog Posts - var postRet = db.Query("SELECT * FROM blog WHERE author_id={0}", new object[] { userId }); + var postRet = db.Query("SELECT * FROM `blog` WHERE `author_id` = {0}", new object[] { oldUserId }); if (postRet != null) { foreach (var post in postRet) @@ -137,7 +168,7 @@ namespace Teknik.Migrations Areas.Blog.Models.BlogPost newPost = new Areas.Blog.Models.BlogPost(); if (post["user_id"].ToString() == "0") { - newPost.BlogId = 0; + newPost.BlogId = config.BlogConfig.ServerBlogId; newPost.System = true; } else @@ -145,12 +176,18 @@ namespace Teknik.Migrations newPost.BlogId = blogId; } newPost.DatePosted = DateTime.Parse(post["date_posted"].ToString()); - newPost.DatePublished = DateTime.Parse(post["date_published"].ToString()); - newPost.DateEdited = DateTime.Parse(post["date_published"].ToString()); + DateTime publishDate = DateTime.Now; + DateTime.TryParse(post["date_published"].ToString(), out publishDate); + if (publishDate < newPost.DatePosted) + { + publishDate = newPost.DatePosted; + } + newPost.DatePublished = publishDate; + newPost.DateEdited = publishDate; newPost.Published = (post["published"].ToString() == "1"); newPost.Title = post["title"].ToString(); newPost.Article = post["post"].ToString(); - context.BlogPosts.Add(newPost); + context.BlogPosts.AddOrUpdate(newPost); context.SaveChanges(); postMapping.Add(Int32.Parse(post["id"].ToString()), newPost.BlogPostId); } @@ -158,7 +195,7 @@ namespace Teknik.Migrations } // Transfer Blog Comments - var commentRet = db.Query("SELECT * FROM comments WHERE service = 'blog'"); + var commentRet = db.Query("SELECT * FROM `comments` WHERE `service` = 'blog'"); foreach (var comment in commentRet) { int postId = Int32.Parse(comment["reply_id"].ToString()); @@ -171,13 +208,13 @@ namespace Teknik.Migrations newComment.Article = comment["post"].ToString(); newComment.DatePosted = DateTime.Parse(comment["date_posted"].ToString()); newComment.DateEdited = DateTime.Parse(comment["date_posted"].ToString()); - context.BlogComments.Add(newComment); + context.BlogComments.AddOrUpdate(newComment); context.SaveChanges(); } } // Transfer Pastes - var pasteRet = db.Query("SELECT * FROM paste"); + var pasteRet = db.Query("SELECT * FROM `paste`"); foreach (var paste in pasteRet) { // If it's a password protected paste, we just skip it @@ -194,11 +231,16 @@ namespace Teknik.Migrations { newPaste.UserId = userMapping[userId]; } - context.Pastes.Add(newPaste); + context.Pastes.AddOrUpdate(newPaste); context.SaveChanges(); } } } } + + private void Db_MysqlErrorEvent(object sender, string e) + { + throw new NotImplementedException(); + } } }