Example #1
0
        public MembershipCreateStatus CreateUser(UserModel userModel, out string error)
        {
            // Check if there is already someone from that organization registered with the system
            error = string.Empty;
            var registerUser = GetUsers().FirstOrDefault(u => u.OrganizationName == userModel.OrganizationName && u.Role == RoleTypes.TeamLeader);

            if (registerUser != null && userModel.Role == RoleTypes.TeamLeader)
            {
                var fullName = string.Format("{0} {1}", registerUser.GivenName, registerUser.FamilyName);
                error =
                    string.Format(
                        "{0} has already registered for {1}. Please contact {0} to create an account for you", fullName,
                        registerUser.OrganizationName);
                return MembershipCreateStatus.UserRejected;
            }

            // Check if we have that organization registered
            var organization = _context.Organizations.FirstOrDefault(o => o.Name == userModel.OrganizationName);

            if (organization == null)
            {
                _context.Organizations.Add(new Organization { Name = userModel.OrganizationName, OrganizationId = Guid.NewGuid() });
                _context.SaveChanges();

                organization = _context.Organizations.FirstOrDefault(o => o.Name == userModel.OrganizationName);
            }

            MembershipCreateStatus createStatus;
            var newUser = Membership.CreateUser(userModel.Email, userModel.Password, userModel.Email, passwordQuestion: null, passwordAnswer: null, isApproved: true, providerUserKey: null, status: out createStatus);

            if (createStatus == MembershipCreateStatus.Success)
            {
                Roles.AddUserToRole(userModel.Email, userModel.Role);

                _context.UserProfiles.Add(new UserProfile
                    {
                        UserProfileId = Guid.NewGuid(),
                        UserId = (Guid)newUser.ProviderUserKey,
                        GivenName = userModel.GivenName,
                        FamilyName = userModel.FamilyName,
                        Phone = userModel.Phone,
                        OrganizationId = organization.OrganizationId

                    });

                if (_context.BikePlanApplications.FirstOrDefault(b => b.OrganizationId == organization.OrganizationId) == null)
                {
                    _context.BikePlanApplications.Add(new BikePlanApplication { BikePlanApplicationId = Guid.NewGuid(), OrganizationId = organization.OrganizationId });
                }

                _context.SaveChanges();
            }

            return createStatus;
        }
Example #2
0
        public bool UpdateUser(UserModel userModel, out string error)
        {
            error = string.Empty;
            try
            {
                var entityToUpdate = _context.Users.FirstOrDefault(u => u.UserId == userModel.UserId);
                var userProfile = _context.UserProfiles.FirstOrDefault(u => u.UserId == userModel.UserId);
                if (entityToUpdate != null)
                {
                    var user = Membership.GetUser(entityToUpdate.UserName);
                    if (user != null)
                    {
                        user.Email = userModel.Email;
                        //user.ResetPassword();
                        Membership.UpdateUser(user);
                    }

                    entityToUpdate.UserName = userModel.Email;
                }
                if (userProfile != null)
                {
                    var userRoles = Roles.GetRolesForUser(userModel.Email);
                    if (!userRoles.Contains(userModel.Role))
                    {
                        Roles.RemoveUserFromRoles(userModel.Email, userRoles);
                        Roles.AddUserToRole(userModel.Email, userModel.Role);
                    }
                    userProfile.Phone = userModel.Phone;
                    userProfile.FamilyName = userModel.FamilyName;
                    userProfile.GivenName = userModel.GivenName;
                    userProfile.OrganizationId = userModel.OrganizationId;
                    userProfile.Company = userModel.Company;
                }

                _context.SaveChanges();

                return true;
            }
            catch (Exception ex)
            {
                error = ((ex.InnerException).InnerException).Message;
                return false;
            }
        }