private HttpResponseMessage ProcessNewUserRecord(HttpRequestMessage request, UserDTO uDto, string key, int companyId, int userId)
        {
            var ur = new UserRepository();

            //var userRepository = new AppUserRepository();
            var user = new User();
            bool newfromsetup;
            if (uDto.CompanyId == null)
            {
                uDto.CompanyId = companyId.ToString();
                newfromsetup = false;
            }
            else
            {
                newfromsetup = true;
            }
            //int? companyIdx = -1;
            if (ur.GetByEmail(uDto.EmailAddress) != null)
            {

                var msg = "Unable to add new user.  Email Address already Exists.";
                return Request.CreateResponse(HttpStatusCode.BadRequest, msg);
            }
            var validationErrors = GetValidationErrors(ur, user, uDto, companyId, userId);

            if (validationErrors.Any())
            {
                return ProcessValidationErrors(request, validationErrors, key);
            }
            //  no validation errors...
            //if (uDto.Password != "sg")
            //{
            //    user.Password = SecurityUtils.GetBinaryPassword(uDto.Password.ToString());
            //}
            //else
            //{
            //    SGApp.Models.EF.User userexist = userRepository.GetUser(uDto.EmailAddress, SecurityUtils.GetBinaryPassword(uDto.Password), ref companyIdx);
            //    user.Password = userexist.Password;
            //}
            //user.CompanyId = companyId;
            //user.UserId = null;
            user = ur.Save(user);
            if (newfromsetup)
            {
                var rr = new RoleRepository();
                var roles = rr.GetRoles();
                foreach (Role role in roles)
                {
                    var aur = new AppUserRoleRepository();
                    var urole = new UserRole();
                    //ur = aur.GetByUserAndRoleIds(contactId, int.Parse(cqDto.RoleID));
                    urole.UserId = user.UserId;
                    urole.RoleId = role.RoleId;
                    aur.Save(urole);
                }
            }
            uDto.Key = key;
            uDto.UserId = user.UserId.ToString();
            var response = request.CreateResponse(HttpStatusCode.Created, uDto);
            response.Headers.Location = new Uri(Url.Link("Default", new
            {
                id = user.UserId
            }));
            return response;
        }
 private HttpResponseMessage RemoveUserRole(HttpRequestMessage request, UserRoleDTO cqDto, int contactId, string key, int companyId, int userId)
 {
     var aur = new AppUserRoleRepository();
     var ur = new UserRole();
     ur = aur.GetByUserAndRoleIds(contactId, int.Parse(cqDto.RoleID));
     aur.Delete(ur);
     cqDto.Key = key;
     return request.CreateResponse(HttpStatusCode.Accepted, cqDto);
 }
        private HttpResponseMessage ProcessExistingUserRecord(HttpRequestMessage request, UserDTO cqDto, int contactId, string key, int companyId, int userId)
        {
            var ur = new UserRepository();
            //var userRepository = new AppUserRepository();
            var user = new User();
            user = ur.GetById(contactId);
            //int? companyIdx = -1;
            //  is the user eligible to update the prospect?
            bool newfromsetup;
            if (cqDto.CompanyId == null)
            {
                cqDto.CompanyId = companyId.ToString();
                newfromsetup = false;
            }
            else
            {
                newfromsetup = true;
            }

                var validationErrors = GetValidationErrors(ur, user, cqDto, companyId, userId);
                if (validationErrors.Any())
                {
                    return ProcessValidationErrors(request, validationErrors, key);
                }
                //  no validation errors...
                //if (cqDto.Password != "sg")
                //{
                //    user.Password = SecurityUtils.GetBinaryPassword(cqDto.Password.ToString());
                //}
                //else
                //{
                //    SGApp.Models.EF.User userexist = userRepository.GetUser(cqDto.EmailAddress, SecurityUtils.GetBinaryPassword(cqDto.Password), ref companyIdx);
                //    user.Password = userexist.Password;
                //}

                ur.Save(user);
                if (newfromsetup)
                {
                    var rr = new RoleRepository();
                    var roles = rr.GetRoles();
                    foreach (Role role in roles)
                    {
                        var aur = new AppUserRoleRepository();
                        var urole = new UserRole();
                        //ur = aur.GetByUserAndRoleIds(contactId, int.Parse(cqDto.RoleID));
                        urole.UserId = user.UserId;
                        urole.RoleId = role.RoleId;
                        aur.Save(urole);
                    }
                }
                cqDto.Key = key;
                return request.CreateResponse(HttpStatusCode.Accepted, cqDto);
        }