/// <summary> /// Creating super admin /// </summary> public void CreateSuperAdmin() { Users user; this._usersColl = _db.GetCollection <Users>(typeof(Users).Name); var dbset = typeof(Users).Name; var collections = _db.ListCollectionNames().ToList(); if (!collections.Any(x => x == dbset)) { var firstName = _config.GetSection("DBInit").GetSection("SuperUser").GetValue <string>("FirstName"); var lastName = _config.GetSection("DBInit").GetSection("SuperUser").GetValue <string>("LastName"); var email = _config.GetSection("DBInit").GetSection("SuperUser").GetValue <string>("Email"); var passcode = AppUtility.Encrypt(_config.GetSection("DBInit").GetSection("SuperUser").GetValue <string>("Password")); user = new Users { FirstName = firstName, LastName = lastName, Email = email, Password = passcode, IsUserVerified = true, CreatedDate = DateTime.Now, CreatedBy = AppUtility.DefaultCreatedBy, RoleId = AdminRoleId }; _usersColl.InsertOne(user); } }
public async Task <IActionResult> Index() { Random generator = new Random(); int code = generator.Next(100000, 1000000); TempData["verifycode"] = code; if (ModelState.IsValid) { var email = registerVM.Email; try { #region register user _user = _userCollection.Find(e => e.Email == email).FirstOrDefault(); if (_user != null) { ModelState.AddModelError(string.Empty, "Email already exists"); return(View()); } _user = new Users() { FirstName = registerVM.FirstName, LastName = registerVM.LastName, Email = registerVM.Email, Password = AppUtility.Encrypt(registerVM.Password), RoleId = _userRoleCollection.Find(e => e.Role == AppUtility.EarnerRole).FirstOrDefault().RoleId.ToString(), CreatedDate = DateTime.Now, CreatedBy = AppUtility.DefaultCreatedBy, IsUserVerified = false }; _userCollection.InsertOne(_user); SolrUsersModel su = new SolrUsersModel(_user); _solr.Add(su); _solr.Commit(); #endregion } catch (Exception e) { ErrorMessage = "Please try again later."; _logger.LogError("RegisterError", e); return(RedirectToAction(nameof(Index))); } await _emailSender.SendEmailAsync(_user.Email, "Confirm your email", $"Your verification code is {code}. Please enter to confirm your email"); TempData["email"] = _user.Email; return(LocalRedirect("/Identity/Account/Verifycode")); } // If we got this far, something failed, redisplay form return(RedirectToAction(nameof(Index))); }
public IActionResult Edit(string id, CreateUser user) { if (ModelState.IsValid) { ObjectId userId = new ObjectId(id); Users _user = usersCollection.Find(e => e.UserId == userId).FirstOrDefault(); var userRoleId = _user.RoleId; var userRole = userRoleCollection.Find(e => e.RoleId == new ObjectId(userRoleId)).FirstOrDefault().Role; var filterForUser = Builders <Users> .Filter.Eq("UserId", userId); var updateUsers = Builders <Users> .Update.Set("FirstName", user.users.FirstName); updateUsers = updateUsers.Set("LastName", user.users.LastName); updateUsers = updateUsers.Set("Email", user.users.Email); updateUsers = updateUsers.Set("RoleId", user.users.RoleId); updateUsers = updateUsers.Set("Password", AppUtility.Encrypt(user.users.Password)); updateUsers = updateUsers.Set("CreatedBy", _user.CreatedBy); updateUsers = updateUsers.Set("CreatedDate", _user.CreatedDate); var result = usersCollection.UpdateOne(filterForUser, updateUsers); if (userRole == AppUtility.IssuerRole) { var filterForIssuer = Builders <Issuers> .Filter.Eq("UserId", userId); var updateIssuer = Builders <Issuers> .Update.Set("Name", user.users.FirstName + " " + user.users.LastName); var resultForIssuers = issuersCollection.UpdateOne(filterForIssuer, updateIssuer); } _user.UserId = userId; _user.FirstName = user.users.FirstName; _user.LastName = user.users.LastName; _user.Email = user.users.Email; _user.RoleId = user.users.RoleId; _user.Password = user.users.Password; _user.CreatedBy = user.users.CreatedBy; _user.CreatedDate = user.users.CreatedDate; SolrUsersModel su = new SolrUsersModel(_user); _solr.Add(su); _solr.Commit(); return(RedirectToAction("Index")); } return(View()); }
public async Task <IActionResult> Create(Users users) { if (ModelState.IsValid) { var claimsIdentity = (ClaimsIdentity)User.Identity; var claim = claimsIdentity.Claims.ToArray(); var loginUserEmail = claim[1].Value; var userName = claim[0].Value; var IsEmailExist = usersCollection.Find(e => e.Email == users.Email).ToList(); if (IsEmailExist.Count() > 0) { ModelState.AddModelError(string.Empty, "User with this email already exist"); CreateUser createUser = new CreateUser() { userRoles = userRoleCollection.Find(role => role.Role != AppUtility.IssuerRole).ToList() }; return(View(createUser)); } DateTime today = DateTime.Now; var password = AppUtility.Encrypt(users.Password); users.CreatedDate = today; users.CreatedBy = userName; users.Password = password; users.IsUserVerified = true; usersCollection.InsertOne(users); SolrUsersModel su = new SolrUsersModel(users); _solr.Add(su); _solr.Commit(); await _emailSender.SendEmailAsync(users.Email, "Congratulation, Now you can use DigiBadges", $"LoginId: {users.Email}<br/>Password: {users.Password}" ); return(RedirectToAction("Index")); } return(View()); }
public IActionResult OnPostSuccessAsync() { if (ModelState.IsValid) { try { var filter = Builders <Users> .Filter.Eq("Email", Email); var updateDef = Builders <Users> .Update.Set("Password", AppUtility.Encrypt(Input.Password)); var result = collection.UpdateOne(filter, updateDef); return(RedirectToPage("./ResetPasswordConfirmation")); } catch (Exception e) { TempData["Email"] = Email; ModelState.AddModelError(string.Empty, "Please try again later."); _logger.LogError("ResetPasswordError", e); return(Page()); } } return(Page()); }
public async Task <IActionResult> Index(StaffUsers staff) { try { //get the current issuer id var userid = User.Claims.FirstOrDefault(c => c.Type == AppUtility.UserId).Value; // Create object of staffUsers model StaffUsers staffUsers = new StaffUsers(); staff.Users.IsUserVerified = true; staff.Users.Password = AppUtility.Encrypt("Welcome@123"); staff.Users.CreatedDate = DateTime.Now; // find the issuer in the collection var issuerModel = collection.Find(e => e.UserId == new ObjectId(userid)).FirstOrDefault(); if (issuerModel != null) { staff.Users.CreatedBy = issuerModel.Name; } //check email of users already exists var IsemailExists = Users.Find(e => e.Email == staff.Users.Email).ToList(); if (IsemailExists.Count() > 0) { ModelState.AddModelError(string.Empty, "User with this email already exist"); var userrolelist = UserRoles.Find(FilterDefinition <UserRoles> .Empty).ToList(); StaffUsers staffUsers1 = new StaffUsers(); staffUsers1.UserRoles = userrolelist; return(View(staffUsers1)); } var useremail = Users.Find(e => e.Email == staff.Users.Email).FirstOrDefault(); if (useremail == null) { Users.InsertOne(staff.Users); // insert staff in user collection SolrUsersModel su = new SolrUsersModel(staff.Users); _solr.Add(su); _solr.Commit(); var staffid = Users.Find(e => e.UserId == staff.Users.UserId).FirstOrDefault(); //get the staffid in user collections string[] ids = new[] { staffid.UserId.ToString() }; Users[] staffobject = new[] { staff.Users }; //get the issuer in the issuer collection var issuerModelnew = collection.Find(e => e.UserId == new ObjectId(userid)).FirstOrDefault(); if (issuerModelnew.StaffsIds != null && issuerModelnew.Staffsobject != null) { collection.UpdateOneAsync(x => x.UserId == new MongoDB.Bson.ObjectId(userid), Builders <Issuers> .Update.PushEach(x => x.StaffsIds, ids)).ConfigureAwait(false); // push the staff id into the array of issuer staffids collection.UpdateOneAsync(x => x.UserId == new MongoDB.Bson.ObjectId(userid), Builders <Issuers> .Update.PushEach(x => x.Staffsobject, staffobject)).ConfigureAwait(false); } else { var filter = Builders <Issuers> .Filter.Eq("UserId", new ObjectId(userid)); var updateDef = Builders <Issuers> .Update. Set("StaffsIds", ids); updateDef = updateDef. Set("Staffsobject", staffobject); collection.UpdateOne(filter, updateDef); // update the staffids column } if (issuerModel.StaffsIds != null && issuerModel.Staffsobject != null) { string[] staffArr = new string[issuerModel.StaffsIds.Length + 1]; Users[] staffObjArr = new Models.Users[issuerModel.Staffsobject.Length + 1]; for (int i = 0; i <= issuerModel.StaffsIds.Length - 1; i++) { staffArr[i] = issuerModel.StaffsIds[i]; staffObjArr[i] = issuerModel.Staffsobject[i]; } staffArr[issuerModel.StaffsIds.Length] = ids[0]; staffObjArr[issuerModel.Staffsobject.Length] = staffobject[0]; issuerModel.StaffsIds = staffArr; issuerModel.Staffsobject = staffObjArr; } else { issuerModel.StaffsIds = ids; issuerModel.Staffsobject = staffobject; } issuerModelnew.UserId = new ObjectId(userid); // SolrIssuersModel sissuser = new SolrIssuersModel(issuerModelnew); // _solrIssuer.Add(sissuser); // _solrIssuer.Commit(); //send the email to the created staff await _emailSender.SendEmailAsync(staff.Users.Email, "Congratulation, you are invited as a staff", $"<h3 style = 'color:blueviolet' >Congratulation, you are invited.. for login</h3><div class='text-center'><a class='btn btn-secondary' href='http://digibadge.eastus.cloudapp.azure.com/Auth/Login'>Login your Account</a></div><br />" + $"" + $"<br/><h2>Your id - {staff.Users.Email}</h2><br/><h2>Your Password - {"Welcome@123"}</h2><br/></div><div class='col-3'></div></div>" ); } // email exists then push the staff id into the array else { var staffid = Users.Find(e => e.UserId == useremail.UserId).FirstOrDefault(); string[] ids = new[] { staffid.UserId.ToString() }; Users[] staffobject = new[] { staff.Users }; var issuerModel1 = collection.Find(e => e.UserId == new ObjectId(userid)).FirstOrDefault(); if (issuerModel1.StaffsIds != null) { collection.UpdateOneAsync(x => x.UserId == new MongoDB.Bson.ObjectId(userid), Builders <Issuers> .Update.PushEach(x => x.StaffsIds, ids)).ConfigureAwait(false); collection.UpdateOneAsync(x => x.UserId == new MongoDB.Bson.ObjectId(userid), Builders <Issuers> .Update.PushEach(x => x.Staffsobject, staffobject)).ConfigureAwait(false); } else { var filter = Builders <Issuers> .Filter.Eq("UserId", new ObjectId(userid)); var updateDef = Builders <Issuers> .Update. Set("StaffsIds", ids); updateDef = updateDef. Set("Staffsobject", staffobject); collection.UpdateOne(filter, updateDef); } /* SolrUsersModel su = new SolrUsersModel(staff.Users); * _solr.Add(su); * _solr.Commit();*/ if (issuerModel1.StaffsIds != null && issuerModel1.Staffsobject != null) { string[] staffArr = new string[issuerModel1.StaffsIds.Length + 1]; Users[] staffObjArr = new Models.Users[issuerModel1.Staffsobject.Length + 1]; for (int i = 0; i <= issuerModel1.StaffsIds.Length - 1; i++) { staffArr[i] = issuerModel1.StaffsIds[i]; staffObjArr[i] = issuerModel1.Staffsobject[i]; } staffArr[issuerModel1.StaffsIds.Length] = ids[0]; staffObjArr[issuerModel1.Staffsobject.Length] = staffobject[0]; issuerModel1.StaffsIds = staffArr; issuerModel1.Staffsobject = staffObjArr; } else { issuerModel1.StaffsIds = ids; issuerModel1.Staffsobject = staffobject; } issuerModel1.UserId = new ObjectId(userid); // SolrIssuersModel sissuser = new SolrIssuersModel(issuerModel1); //_solrIssuer.Add(sissuser); //_solrIssuer.Commit(); //send email to the created staff await _emailSender.SendEmailAsync(staff.Users.Email, "Congratulation, you are invited as a staff", $"<h3 style = 'color:blueviolet' >Congratulation, you are invited.. for login</h3><div class='text-center'><a class='btn btn-secondary' href='http://digibadge.eastus.cloudapp.azure.com/Auth/Login'>Login your Account</a></div><br />" + $"" + $"<br/><h2>Your id - ${staff.Users.Email}</h2><br/><h2>Your Password - ${"Welcome@123"}</h2><br/></div><div class='col-3'></div></div>" ); } } catch (Exception) { ModelState.AddModelError(string.Empty, "Please try again later."); return(View()); } return(RedirectToAction("ViewStaff")); }
public async Task <IActionResult> CreateAsync(Issuers issuers) { if (ModelState.IsValid) { try { var claimsIdentity = (ClaimsIdentity)User.Identity; var claim = claimsIdentity.Claims.ToArray(); var useid = User.Claims.FirstOrDefault(c => c.Type == AppUtility.UserId).Value; //to get userId of loggedIn user var userRole = _userRoles.AsQueryable().ToList(); //to get userRoleList var issuerRoles = userRole.Where(e => e.Role == AppUtility.IssuerRole).FirstOrDefault(); //find the object of issuer role string webRootPath = _hostEnvironment.WebRootPath; var files = HttpContext.Request.Form.Files; if (files.Count > 0) { string fileName = Guid.NewGuid().ToString(); var uploads = Path.Combine(webRootPath, @"Images\issuers"); var extenstion = Path.GetExtension(files[0].FileName); using (var filesStreams = new FileStream(Path.Combine(uploads, fileName + extenstion), FileMode.Create)) { files[0].CopyTo(filesStreams); } issuers.Image = @"\images\issuers\" + fileName + extenstion; } DateTime today = DateTime.Now; var isEmailExistInUser = _user.FilterBy(e => e.Email == issuers.Email).ToList(); var isEmailExistInIssuer = collection.Find(e => e.Email == issuers.Email).ToList(); if (isEmailExistInIssuer.Count() > 0 || isEmailExistInUser.Count() > 0) { ModelState.AddModelError(string.Empty, "User with this email already exist"); //show popup if same email exists return(View()); } DigiBadges.DataAccess.Users user = new DigiBadges.DataAccess.Users() { CreatedBy = claim[0].Value, CreatedDate = today, Email = issuers.Email, FirstName = issuers.Name, IsUserVerified = true, Password = AppUtility.Encrypt(AppUtility.IssuerPassword), RoleId = issuerRoles.Id.ToString() }; _user.InsertOne(user); //Inserting object in issuer table DigiBadges.Models.Users users = new DigiBadges.Models.Users() { CreatedBy = claim[0].Value, CreatedDate = today, Email = issuers.Email, FirstName = issuers.Name, IsUserVerified = true, Password = AppUtility.Encrypt(AppUtility.IssuerPassword), RoleId = issuerRoles.Id.ToString(), UserId = user.Id }; SolrUsersModel su = new SolrUsersModel(users); _solr.Add(su); //Adding data in solr _solr.Commit(); var userIdInUserTable = _user.AsQueryable().ToList(); var uid = userIdInUserTable.Where(e => e.Email == issuers.Email).FirstOrDefault(); if (user.Id != null) { issuers.UserId = user.Id; //setting the userId which we got after inserting the above data in user collection issuers.CreatedDate = today; } collection.InsertOne(issuers); //To post the issuer object SolrIssuersModel sissuser = new SolrIssuersModel(issuers); _solrIssuer.Add(sissuser); //Adding data in solr _solrIssuer.Commit(); await _emailSender.SendEmailAsync(issuers.Email, //to send email to new issuer "Congatulations you are invited as a issuer", $"<h4 class='m-2'>Your Email id is -{HtmlEncoder.Default.Encode(issuers.Email)}</h4></div>" + "Your password is - Welcome@123"); } catch (Exception) { ModelState.AddModelError(string.Empty, "Please try again later."); return(View()); } return(RedirectToAction("Index")); } return(View()); }
public async Task <IActionResult> Index() { try { if (ModelState.IsValid) { var email = Input.Email; var password = AppUtility.Encrypt(Input.Password); Users user = collection.Find(e => e.Email == email && e.Password == password).FirstOrDefault(); if (user != null && !string.IsNullOrEmpty(Input.Password)) { if (user.IsUserVerified) { var role = _userRoleCollection.Find(e => e.RoleId == ObjectId.Parse(user.RoleId)).FirstOrDefault().Role; #region claims await SetClaims(user, role, Input.RememberMe); #endregion _logger.LogInformation("User {Email} logged in at {Time}.", user.Email, DateTime.UtcNow); if (Input.RememberMe) { var option = new CookieOptions(); option.Expires = DateTime.Now.AddDays(1); Response.Cookies.Append("Email", Input.Email, option); } else { Response.Cookies.Delete("Email"); } switch (role) { /*Admin*/ case AppUtility.AdminRole: return(RedirectToAction("Index", "Users", new { area = "Admin" })); /*Earner*//*Issuer*/ default: return(RedirectToAction("Index", "Home", new { area = "Employee" })); } } else { Random generator = new Random(); int code = generator.Next(100000, 1000000); await _emailSender.SendEmailAsync(user.Email, "Confirm your email", $"Your verification code is {code}. Please enter to confrim your email"); TempData["email"] = user.Email; TempData["verifycode"] = code; return(LocalRedirect("/Identity/Account/Verifycode")); } } else { ModelState.AddModelError(string.Empty, "Invalid Credentials!"); } } // If we got this far, something failed, redisplay form return(View()); } catch (Exception e) { _logger.LogError("LoginError", e); ModelState.AddModelError(string.Empty, "Please try again later."); return(View()); } }