Exemplo n.º 1
0
        //this is the only request that does not require admin role
        public JarsUserResponse Any(GetJarsUser request)
        {
            if (request.EmailOrUserName.IsNullOrEmpty())
            {
                return(null);
            }

            var             sessionUserName  = Request.GetSession().UserName;
            var             sessionUserEmail = Request.GetSession().Email;
            IAuthRepository ssAuthRepo       = ServiceStackHost.Instance.GetAuthRepository();
            IUserAuth       ssUser           = ssAuthRepo.GetUserAuthByUserName(request.EmailOrUserName);

            if (ssUser == null)
            {
                throw HttpError.NotFound("User not found");
            }
            if (ssUser.LockedDate != null)
            {
                throw HttpError.Unauthorized("User account locked");
            }

            if (ssUser.Roles.Count == 0 || ssUser.Permissions.Count == 0)
            {
                IUserAuth newUserA = new UserAuth();
                newUserA.PopulateWith(ssUser);
                if (ssUser.Roles.Count == 0)
                {
                    newUserA.Roles.Add("Guest");
                }
                if (ssUser.Permissions.Count == 0)
                {
                    newUserA.Permissions.Add("ViewOnly");
                }
                ssUser = ssAuthRepo.UpdateUserAuth(ssUser, newUserA);
            }

            IJarsUserRepository repository = _DataRepositoryFactory.GetDataRepository <IJarsUserRepository>();
            JarsUser            acc        = repository.Where(u => u.UserName == ssUser.UserName || u.Email == ssUser.Email, request.FetchEagerly).SingleOrDefault();

            if (acc == null)
            {
                acc    = ssUser.ConvertTo <JarsUser>();
                acc.Id = 0;
                acc    = repository.CreateUpdate(acc, sessionUserName);
            }
            else
            {
                //we have to change the id because the 2 tables differ and id's wont match.
                int accId = acc.Id;
                acc.PopulateWith(ssUser);
                acc.Id = accId;
                acc    = repository.CreateUpdate(acc, sessionUserName);
            }

            JarsUserResponse response = new JarsUserResponse
            {
                UserAccount = acc.ConvertTo <JarsUserDto>()
            };

            //response.jarsUserAccount = FakeDataHelper.FakeUserAccount;
            return(response);
        }