Exemplo n.º 1
0
        private void SeedDefaultUsers()
        {
            if (!_context.Users.Any(m => m.Email == masterUserEmail))
            {
                var masterUser = new CommonUser();

                masterUser.Email = masterUserEmail;
                masterUser.SetPassword("Olympious911!");
                masterUser.Permission = SystemPermissions.Administrator;

                _context.Users.Add(masterUser);
                _context.SaveChanges();
            }
        }
Exemplo n.º 2
0
        public async Task <IActionResult> Register([FromBody] UserRegisterModel model)
        {
            var response = new APIResponse <TokenResponseModel>();

            if (ModelState.IsValid)
            {
                var userAlreadyExists = await CommonContext.Users.AnyAsync(q => q.Email.ToLower() == model.Email.ToLower());

                if (userAlreadyExists)
                {
                    // This isn't a security risk because we've verified the email address already
                    response.Success = false;
                    response.Errors.Add(new Error {
                        Code = Common.Enums.ErrorCode.None, Message = "A user has already verified that email address."
                    });
                }
                else
                {
                    var user = new CommonUser();
                    user.Email = model.Email;
                    user.SetPassword(model.Password);
                    user.Permission = IsAdmin(model.Email);


                    //put dude in the database
                    using (var tx = CommonContext.Database.BeginTransaction())
                    {
                        CommonContext.Users.Add(user);
                        await CommonContext.SaveChangesAsync();

                        tx.Commit();
                    }

                    //Register user device
                    var device = await RegisterUserDevice(model, user);

                    //Return JWT Token
                    return(Ok(await CreateEncodedJwtToken(user)));
                }
            }
            else
            {
                response.Success = false;
                response.Errors.AddRange(ModelState.ToErrors());
            }
            return(Ok(response));
        }
Exemplo n.º 3
0
        public async Task <IActionResult> Create(UserViewModel model)
        {
            if (ModelState.IsValid)
            {
                var userAlreadyExists = await CommonContext.Users.AnyAsync(q => q.Email.ToLower() == model.Email.ToLower());

                if (userAlreadyExists)
                {
                    // This isn't a security risk because we've verified the email address already
                    ModelState.AddModelError(string.Empty, "This email address already exists.");
                    PopulateDropDownUsers(model);
                    return(View(model));
                }

                var user = new CommonUser();
                user.Email     = model.Email;
                user.FirstName = model.FirstName;
                user.LastName  = model.LastName;
                user.SetPassword(model.Password);
                user.Permission   = model.Permission;
                user.CreateUserId = User.GetLoggedInUserId().Value;
                user.UpdateUserId = user.CreateUserId = User.GetLoggedInUserId().Value;


                //put dude in the database
                using (var tx = CommonContext.Database.BeginTransaction())
                {
                    CommonContext.Users.Add(user);
                    await CommonContext.SaveChangesAsync();

                    tx.Commit();
                }
                return(RedirectToAction("Index"));
            }

            //, "users", new { Area = Area.Admin });
            PopulateDropDownUsers(model);
            return(View(model));
        }
        public async Task <ActionResult> Register(UserRegisterModel model)
        {
            if (ModelState.IsValid)
            {
                var userAlreadyExists = await CommonContext.Users.AnyAsync(q => q.Email.ToLower() == model.Email.ToLower());

                if (userAlreadyExists)
                {
                    // This isn't a security risk because we've verified the email address already
                    ModelState.AddModelError(string.Empty, "A user has already verified that email address.");
                }
                else
                {
                    var user = new CommonUser();

                    user.Email = model.Email;
                    user.SetPassword(model.Password);
                    user.Permission = IsAdmin(model.Email);

                    //put dude in the database
                    using (var tx = CommonContext.Database.BeginTransaction())
                    {
                        CommonContext.Users.Add(user);
                        await CommonContext.SaveChangesAsync();

                        tx.Commit();

                        await SignInUser(user, false);

                        return(RedirectToAction("Index", "Accounts", new { Area = Area.Admin }));
                    }
                }
            }

            return(View(model));
        }
        public async Task <ActionResult> CreatePassword(CreatePassword model)
        {
            if (ModelState.IsValid)
            {
                //Create new User and Account
                var user = new CommonUser();
                user.Email      = model.InvitedEmail;
                user.FirstName  = model.FirstName;
                user.LastName   = model.LastName;
                user.Permission = model.SystemPermission;
                user.SetPassword(model.Password);


                //Check Account
                var commonAccount = await CommonContext.CommonAccounts.Where(m => m.Id == model.AccountId).SingleOrDefaultAsync();

                var vendorDetail = await _accountCtx.Vendors.Where(x => x.Id == model.VendorId).SingleOrDefaultAsync();


                if (commonAccount == null)
                {
                    ViewBag.SucessMessage = "Account not found";
                    return(View(model));
                }

                CommonUserAccount userAccount = new CommonUserAccount();
                userAccount.CreateUserId = commonAccount.OwnerUserId.Value;
                userAccount.AccountId    = commonAccount.Id;
                userAccount.UpdateUserId = commonAccount.OwnerUserId.Value;
                userAccount.UserId       = user.Id;
                userAccount.Permissions  = (AccountPermissions)model.Permission;

                if (vendorDetail != null)
                {
                    user.Permission = SystemPermissions.Vendor;
                }

                using (var tx = CommonContext.Database.BeginTransaction())
                {
                    CommonContext.Users.Add(user);
                    await CommonContext.SaveChangesAsync();

                    CommonContext.UserAccounts.Add(userAccount);
                    await CommonContext.SaveChangesAsync();

                    tx.Commit();
                }

                //Create AccountUser
                var accountUser = new AccountUser
                {
                    Id        = userAccount.UserId.Value,
                    FirstName = user.FirstName,
                    LastName  = user.LastName,
                    Email     = user.Email
                };

                _accountCtx.AccountUsers.Add(accountUser);

                if (vendorDetail != null)
                {
                    AccountUserVendor accountUserVendor = new AccountUserVendor();
                    accountUserVendor.VendorId      = vendorDetail.Id;
                    accountUserVendor.AccountUserId = accountUser.Id;

                    _accountCtx.AccountUserVendors.Add(accountUserVendor);
                }

                await _accountCtx.SaveChangesAsync();

                await SignInUser(user, false);

                return(RedirectToAction("Index", "Accounts"));
            }
            return(View(model));
        }