public ActionResult ImportMembers() { System.Threading.Thread.Sleep(200); CultureInfo provider = CultureInfo.InvariantCulture; List<Member> MemberResult = new List<Member>(); int LineNumber = 1; try { for (int i = 0; i < Request.Files.Count; i++) { String FileContent = ""; HttpPostedFileBase PostedFile = Request.Files[i]; using (System.IO.Stream input = PostedFile.InputStream) { byte[] buffer = new byte[PostedFile.InputStream.Length]; input.Read(buffer, 0, buffer.Length); FileContent = System.Text.ASCIIEncoding.UTF8.GetString(buffer); } if (FileContent.Length > 0) { string[] Lines = FileContent.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries); AuditEvent.AppEventSuccess(Profile.Member.Email, String.Format(AuditEvent.MemberUploadHasStarted, Lines.Length)); int Added = 0; int Failed = 0; int Exists = 0; foreach (String Line in Lines) { System.Threading.Thread.Sleep(1); if (LineNumber == 1 || Line == null || Line.Trim().Length == 0) { LineNumber++; continue; } List<string> Values = new List<string>(); CsvParser parser = new CsvParser(Line); foreach (string s in parser) Values.Add(s.Trim()); if (Values.Count >= 11) { Member Member = Web.Admin.Logic.Collections.Members.GetByEmail(Values[2]); if (Member.MemberID > 0) { Member.UIMessage = "Member already exists."; Member.UILineNumber = LineNumber; Member.UIMessageType = Logic.UIMessageType.Warning; AuditEvent.AppEventWarning(Profile.Member.Email, String.Format(AuditEvent.MemberUploadMemberExists, Member.Name, Member.Email)); Exists++; } else { Member.Name = Values[0]; Member.Email = Values[2]; bool randomPassword = false; if (Values[3] == null || Values[3].Trim().Length == 0) { Member.Password = Web.Admin.Logic.StringTool.RandomString(10); randomPassword = true; } else { Member.Password = Values[3]; } Member.Avatar = Values[4]; if(Values[5] != null && Values[5].Length > 0) { try{ if (Member.Avatar == null || Member.Avatar.Trim().Length == 0) { Member.Avatar = Web.Admin.Logic.StringTool.RandomString(24); } SaveAvatarFromBase64FromByteArray(Member.Avatar, Values[5]); } catch { Member.Avatar = ""; } } if(BuiltInRoleMember.ContainsMember(Member.Name)) Member.IsBuiltIn = true; else Member.IsBuiltIn = false; try { Member.LastLogin = DateTime.ParseExact(Values[6], "dd.MM.yyyy HH:mm:ss", provider); } catch { } try { Member.Created = DateTime.ParseExact(Values[7], "dd.MM.yyyy HH:mm:ss", provider); } catch { } Member.Save(); Added ++; if(Values[1] != null && Values[1].Length > 0) { string[] toRols = Values[1].Split(new string[] { "|" }, StringSplitOptions.RemoveEmptyEntries); foreach (string RoleName in toRols) { Role Role = Roles.GetBy(RoleName); if (Role.RoleID > 0) { MemberRole MemberRole = new MemberRole(); MemberRole.MemberID = Member.MemberID; MemberRole.RoleID = Role.RoleID; MemberRole.Save(); } else { // Create new role ? } } } Member.UIMessage = "Member has been added." + (randomPassword ? " (Random password)" : ""); Member.UIMessageType = Logic.UIMessageType.Success; Member.UILineNumber = LineNumber; AuditEvent.AppEventSuccess(Profile.Member.Email, String.Format(AuditEvent.MemberUploadedItemAdded, Member.Name, Member.Email)); } MemberResult.Add(Member); if (Values[8] != null && Values[8].Trim().Length > 0) { MemberToken Token = MemberTokens.GetByMember(Member.MemberID); if (Token.MemberTokenID <= 0) Token = MemberTokens.GetByToken(Values[8].Trim()); if (Token.MemberTokenID <= 0) { MemberToken MemberToken = new MemberToken(); MemberToken.MemberID = Member.MemberID; MemberToken.Token = Values[8]; MemberToken.GeneratedBy = Values[9]; try { MemberToken.Created = DateTime.ParseExact(Values[10], "dd.MM.yyyy HH:mm:ss", provider); } catch { } try { if (Values.Count == 12) MemberToken.Modified = DateTime.ParseExact(Values[11], "dd.MM.yyyy HH:mm:ss", provider); } catch { } MemberToken.Save(); } else { Member member = Web.Admin.Logic.Collections.Members.GetBy(Token.MemberID); Member.UIMessage = String.Format("Member has been added, but API token \"{0}\" already exists in database for member \"{1}\" <{2}>", Values[8], member.Name, member.Email); Member.UIMessageType = Logic.UIMessageType.Warning; } } } else if (Values.Count > 0) { Member Member = new Member(); Member.UIMessage = "Wrong line format. Number of columns: " + Values.Count.ToString(); Member.UIMessageType = Logic.UIMessageType.ErrorOrDanger; Member.UILineNumber = LineNumber; MemberResult.Add(Member); Failed++; AuditEvent.AppEventSuccess(Profile.Member.Email, String.Format(AuditEvent.MemberUploadWrongLineFormat,LineNumber,Values.Count)); } LineNumber++; } AuditEvent.AppEventSuccess(Profile.Member.Email, String.Format(AuditEvent.MemberUploadHasStarted, Lines.Length, Added, Exists, Failed)); } } System.Threading.Thread.Sleep(1000); MemberListModel Model = new MemberListModel(); Model.Members = MemberResult; return Json(new { NotifyType = NotifyType.Dialog, Html = this.RenderPartialView(@"_ImportMembersResult", Model) }, "text/html", JsonRequestBehavior.AllowGet); } catch (Exception ex) { RequestResultModel _model = new RequestResultModel(); _model.Title = "Warning"; _model.Message = String.Format("Something went wrong: {0}", HttpUtility.HtmlEncode(ex.Message)); _model.InfoType = RequestResultInfoType.ErrorOrDanger; _model.HideInSeconds = 0; _model.Height = 250; AuditEvent.AppEventError(Profile.Member.Email, _model.Message, AuditEvent.GetSessionDetails(ex.StackTrace)); return Json(new { Status = "Ok", NotifyType = NotifyType.DialogInline, Html = this.RenderPartialView(@"_RequestResultDialog", _model) }, "text/html", JsonRequestBehavior.AllowGet); } }
public ActionResult GenerateMemberToken(string Email) { RequestResultModel _model = new RequestResultModel(); Member Member = Web.Admin.Logic.Collections.Members.GetByEmail(Email); if (!AppSession.IsEmail(Email)) { _model.Title = GetLabel("Member.Controler.Warning"); _model.Message = GetLabel("Member.Controler.WrongEmail"); _model.InfoType = RequestResultInfoType.ErrorOrDanger; AuditEvent.AppEventSuccess(Profile.Member.Email, String.Format(AuditEvent.SentRandomPasswordBadFormat, Email)); } else { if (Member.MemberID <= 0) { _model.Title = GetLabel("Member.Controler.Warning"); _model.Message = GetLabel("Member.Controler.AccountNotExists"); _model.InfoType = RequestResultInfoType.Warning; AuditEvent.AppEventSuccess(Profile.Member.Email, String.Format(AuditEvent.SentRandomPasswordNotExist, Email)); } else { string Token = Web.Admin.Logic.StringTool.RandomString(48); bool result = false; MemberToken ExistToken = MemberTokens.GetByMember(Member.MemberID); if (ExistToken.MemberTokenID > 0) { ExistToken.Token = Token; result = ExistToken.SaveToken(); } else { MemberToken _Token = new MemberToken(); _Token.MemberID = Member.MemberID; _Token.GeneratedBy = Profile.Member.Email; _Token.Token = Token; result = _Token.Save(); } if (result) { _model.Title = GetLabel("Member.Controler.Congrat"); _model.Message = GetLabel("Member.Controler.MemberTokenCreated"); _model.InfoType = RequestResultInfoType.Success; AuditEvent.AppEventSuccess(Profile.Member.Email, String.Format("The member \"{0}\"<{1}> token has been created by \"{2}\"<{3}>.", Member.Name, Email,Profile.Member.Name, Profile.Member.Email)); } } } MemberListModel _ListModel = new MemberListModel(); _ListModel.Members.Add(Member); return Json(new { Status = _model.InfoType, NotifyType = NotifyType.DialogInline, Html = this.RenderPartialView(@"_RequestResultDialogInLine", _model), TokenPanel = this.RenderPartialView(@"_MemberToken", _ListModel), }, JsonRequestBehavior.AllowGet); }