public SYS_UserCompany CompanyData(byte companyId, string userId, string userName)
        {
            SYS_UserCompany company = null;

            try
            {
                company = _ctx.UserCompanies.Where(c => c.UserId == userId && c.SYS_Company.CompanyId == companyId &&
                                                   c.SYS_Company.Active && !c.SYS_Company.Canceled).ToList().FirstOrDefault();
            }
            catch (Exception ex)
            {
                _log.Error(ex.Message, ex.Source, ex.StackTrace, userName);
            }

            return(company);
        }
        public void RemoveUserCompany(SYS_UserCompany entity)
        {
            var currentUnitOfWork = (IMainUnitOfWork)UnitOfWork;

            currentUnitOfWork.UsersCompany.Remove(entity);
        }
Example #3
0
        public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
        {
            ApplicationUser user = null;
            SYS_UserCompany userCompany = null;
            string          userId = string.Empty, role = string.Empty;
            string          companyCode   = context.OwinContext.Get <string>("as:companyCode");
            var             allowedOrigin = context.OwinContext.Get <string>("as:clientAllowedOrigin");

            if (allowedOrigin == null)
            {
                allowedOrigin = "*";
            }

            context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { allowedOrigin });
            //context.OwinContext.Response.Headers.Add("Access-Control-Allow-Headers", new[] { "Content-Type" });
            //context.OwinContext.Response.Headers.Add("Access-Control-Allow-Methods", new[] { "GET, POST, PUT, DELETE, OPTIONS" });

            using (AuthRepository _repo = new AuthRepository())
            {
                user = await _repo.FindUser(context.UserName, context.Password);

                if (user == null)
                {
                    context.SetError("invalid_grant", "El usuario o contraseña son incorrectas.");
                    return;
                }

                userId      = user.Id;
                userCompany = _repo.UserInCompany(user.WorkSpaceId, userId, companyCode);

                if (userCompany == null)
                {
                    context.SetError("invalid_companyCode", "El usuario no tiene acceso a la compañia.");
                    return;
                }

                role = await _repo.GetRole(userId);

                if (role.Equals(string.Empty))
                {
                    context.SetError("invalid_role", "El usuario no tiene rol.");
                    return;
                }
            }

            var identity = new ClaimsIdentity(context.Options.AuthenticationType);

            identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, userId));
            identity.AddClaim(new Claim(ClaimTypes.Name, context.UserName));
            identity.AddClaim(new Claim(ClaimTypes.Role, role));
            identity.AddClaim(new Claim("CompanyId", userCompany.CompanyId.ToString()));
            identity.AddClaim(new Claim("WorkSpaceId", user.WorkSpaceId.ToString()));

            var props = new AuthenticationProperties(new Dictionary <string, string>
            {
                {
                    "client_id", (context.ClientId == null) ? string.Empty : context.ClientId
                }
            });

            var ticket = new AuthenticationTicket(identity, props);

            context.Validated(ticket);
        }