public void ChangeUser(string city, string street, int streetnumber, int regionid, string firstname, string surname, string address, string username, string email, string password)
        {
            var adr = new Address();
            adr.City = city;
            adr.Street = street;
            adr.StreetNumber = streetnumber;
            adr.RegionId = regionid;

            this.UnitOfWork.AddressRepository.Insert(adr);

            var pers = new Person();
            pers.FirstName = firstname;
            pers.SurName = surname;
            pers.Address = adr;

            this.UnitOfWork.PersonRepository.Insert(pers);

            var usr = new User();
            usr.UserName = username;
            usr.Password = password;
            usr.RoleId = 1;
            usr.Person = pers;

            this.UnitOfWork.UserRepository.Insert(usr);
        }
        public void CreateUser(string city, string street, int streetnumber, decimal latitude, decimal longitude, int regionid, string firstname, string surname, string username, string email, string password)
        {
            try
            {
                var adr = new Address();
                adr.City = city;
                adr.Street = street;
                adr.StreetNumber = streetnumber;
                adr.Latitude = latitude;
                adr.Longitude = longitude;
                adr.RegionId = regionid;

                this.UnitOfWork.AddressRepository.Insert(adr);

                var pers = new Person();
                pers.FirstName = firstname;
                pers.SurName = surname;
                pers.Address = adr;

                this.UnitOfWork.PersonRepository.Insert(pers);

                var roleid = this.UnitOfWork.RoleRepository.Get().Where(r => r.Name == "Administrator").FirstOrDefault().Id;

                var usr = new User();
                usr.UserName = username;
                usr.PasswordSalt = BCrypt.Net.BCrypt.GenerateSalt(12);
                usr.Password = BCrypt.Net.BCrypt.HashPassword(password, usr.PasswordSalt);
                usr.Rating = 10;
                usr.Email = email;
                usr.RoleId = roleid;
                usr.Person = pers;

                this.UnitOfWork.UserRepository.Insert(usr);
                this.UnitOfWork.Save();
            }
            catch (DbEntityValidationException dbEx)
            {
                foreach (var validationErrors in dbEx.EntityValidationErrors)
                {
                    foreach (var validationError in validationErrors.ValidationErrors)
                    {
                        Trace.TraceInformation("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage);
                    }
                }

            }
        }
示例#3
0
        public ActionResult Create(ReUzze.Models.UserViewModel viewModel)
        {
            try
            {
                // Check if there are roles in the database
                var roles = this.UnitOfWork.RoleRepository.Get().Count();
                if (roles == 0)
                {
                    var model = new ReUzze.Models.UserViewModel
                    {
                        StatusMessage = "There are no roles in the database ...",
                        Roles = new SelectList(this.UnitOfWork.RoleRepository.Get(), "Id", "Name"),
                        Regions = new SelectList(this.UnitOfWork.RegionRepository.Get(), "Id", "Name")
                    };
                    return View(model);
                }

                // Check if there are regions in database
                var regions = this.UnitOfWork.RegionRepository.Get().Count();
                if (regions == 0)
                {
                    var model = new ReUzze.Models.UserViewModel
                    {
                        StatusMessage = "There are no regions in the database ...",
                        Roles = new SelectList(this.UnitOfWork.RoleRepository.Get(), "Id", "Name"),
                        Regions = new SelectList(this.UnitOfWork.RegionRepository.Get(), "Id", "Name")
                    };
                    return View(model);
                }

                // Check if user has selected region
                if (viewModel.SelectRegionId == 0)
                {
                    var model = new ReUzze.Models.UserViewModel
                    {
                        StatusMessage = "You need to select a region!",
                        Roles = new SelectList(this.UnitOfWork.RoleRepository.Get(), "Id", "Name"),
                        Regions = new SelectList(this.UnitOfWork.RegionRepository.Get(), "Id", "Name")
                    };
                    return View(model);
                }

                // Check if user has selected role
                if (viewModel.SelectRoleId == 0)
                {
                    var model = new ReUzze.Models.UserViewModel
                    {
                        StatusMessage = "You need to select a role!",
                        Roles = new SelectList(this.UnitOfWork.RoleRepository.Get(), "Id", "Name"),
                        Regions = new SelectList(this.UnitOfWork.RegionRepository.Get(), "Id", "Name")
                    };
                    return View(model);
                }

                // Check if username already exists
                var usrname = this.UnitOfWork.UserRepository.Get().Where(u => u.UserName == viewModel.UserName).Count();
                if (usrname != 0)
                {
                    var model = new ReUzze.Models.UserViewModel
                    {
                        StatusMessage = "Please choose another username. This one already exists.",
                        Roles = new SelectList(this.UnitOfWork.RoleRepository.Get(), "Id", "Name"),
                        Regions = new SelectList(this.UnitOfWork.RegionRepository.Get(), "Id", "Name")
                    };
                    return View(model);
                }

                // Check if user email already exists
                var email = this.UnitOfWork.UserRepository.Get().Where(u => u.Email == viewModel.Email).Count();
                if (email != 0)
                {
                    var model = new ReUzze.Models.UserViewModel
                    {
                        StatusMessage = "This email address is already registered.",
                        Roles = new SelectList(this.UnitOfWork.RoleRepository.Get(), "Id", "Name"),
                        Regions = new SelectList(this.UnitOfWork.RegionRepository.Get(), "Id", "Name")
                    };
                    return View(model);
                }

                if (!ModelState.IsValid)
                {
                    var model = new ReUzze.Models.UserViewModel
                    {
                        Roles = new SelectList(this.UnitOfWork.RoleRepository.Get(), "Id", "Name"),
                        Regions = new SelectList(this.UnitOfWork.RegionRepository.Get(), "Id", "Name")
                    };
                    return View(model);
                }

                // ADDRESS
                var address = new Address();
                address.City = viewModel.City;
                address.Street = viewModel.Street;
                address.StreetNumber = viewModel.StreetNumber;
                address.Latitude = viewModel.Latitude;
                address.Longitude = viewModel.Longitude;
                address.RegionId = viewModel.SelectRegionId;

                this.UnitOfWork.AddressRepository.Insert(address);

                // PERSON
                var person = new Person();
                person.FirstName = viewModel.FirstName;
                person.SurName = viewModel.SurName;
                person.Address = address;
                this.UnitOfWork.PersonRepository.Insert(person);

                // USER

                var user = new User();
                user.UserName = viewModel.UserName;
                user.Email = viewModel.Email;
                user.PasswordSalt = BCrypt.Net.BCrypt.GenerateSalt(12);
                user.Password = BCrypt.Net.BCrypt.HashPassword(viewModel.Password, user.PasswordSalt);
                user.Rating = 10;
                user.RoleId = viewModel.SelectRoleId;
                user.Person = person;
                this.UnitOfWork.UserRepository.Insert(user);

                // ROLE NEEDS TO BE ADDED TO USER

                this.UnitOfWork.Save();

                return RedirectToAction("Index", "User");
            }
            catch (DbEntityValidationException dbEx)
            {
                foreach (var validationErrors in dbEx.EntityValidationErrors)
                {
                    foreach (var validationError in validationErrors.ValidationErrors)
                    {
                        Trace.TraceInformation("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage);
                    }
                }

            }

            return RedirectToAction("Create", "User");
        }