Exemplo n.º 1
0
        public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
        {
            string rbacName = context.OwinContext.Get <string>("rbac");

            if (string.IsNullOrEmpty(rbacName))
            {
                RbacException.Raise("Parameter rbac was not passed in the request!", RbacExceptionCategories.Web);
            }

            Rbac rbac = new Rbac(context.UserName);
            //RbacUser user = rbac.Authenticate(context.UserName, context.Password);

            var claims = new List <Claim>();

            claims.Add(new Claim(ClaimTypes.Name, rbac.User.UserName));
            claims.Add(new Claim(ClaimTypes.Email, rbac.User.Email));
            claims.Add(new Claim("rbac", rbacName));
            var oAuthIdentity = new ClaimsIdentity(claims, DefaultAuthenticationTypes.ApplicationCookie);

            Dictionary <string, string> kvPair = new Dictionary <string, string>();

            kvPair.Add("rbacid", rbac.RbacId.ToString());
            kvPair.Add("fullname", rbac.User.FullName);
            var props = new AuthenticationProperties(kvPair);


            AuthenticationTicket ticket = new AuthenticationTicket(oAuthIdentity, props);

            context.Validated(ticket);
            context.Request.Context.Authentication.SignIn(oAuthIdentity);
        }
Exemplo n.º 2
0
        private string RaiseError(Exception ex, bool raiseError = true)
        {
            //List<string> errors = new List<string>();

            //try
            //{

            //    foreach (var result in dbu.Entries)
            //    {
            //        errors.Add(string.Format("Type: {0} was part of the problem. ", result.Entity.GetType().Name));
            //    }
            //}
            //catch (Exception e)
            //{
            //    errors.Add(e.ToString());
            //}

            //string errorMessage = errors.Select(i => i).Aggregate((i, j) => i + Environment.NewLine + j);

            List <Exception> exceptions = ex.GetInnerExceptions().ToList();

            exceptions.RemoveAll(e => e.Message == "An error occurred while updating the entries. See the inner exception for details.");
            string errorMessage = exceptions.Select(i => i.Message).Aggregate((i, j) => i + Environment.NewLine + j);

            if (raiseError)
            {
                RbacException.Raise(errorMessage, RbacExceptionCategories.Repository);
            }

            return(errorMessage);
        }
Exemplo n.º 3
0
        public Framework.RbacRole AddOrUpdate(Framework.RbacRole role)
        {
            RbacMetaData rbacMetaData = new RbacMetaData();

            rbacMetaData.ValidateAndGetRbacXmlDocument(role.MetaDataRbac);
            if (rbacMetaData.XmlValidationErrors.Count > 0)
            {
                RbacException.Raise("Cannot save role meta data, XML validation failed!"
                                    + Environment.NewLine
                                    + rbacMetaData.XmlValidationErrors.ToLine());
            }

            rbacMetaData.ValidateAndGetEntitlementXmlDocument(role.MetaDataEntitlements);
            if (rbacMetaData.XmlValidationErrors.Count > 0)
            {
                RbacException.Raise("Cannot save role entitlement meta data, XML validation failed!"
                                    + Environment.NewLine
                                    + rbacMetaData.XmlValidationErrors.ToLine());
            }

            RbacRole dbRole = null;

            try
            {
                using (var ctx = new Entities(ConnectionString))
                {
                    dbRole = ctx.RbacRoles.Where(r => r.RoleId == role.RoleId).SingleOrDefault();
                    if (dbRole == null) //try with name
                    {
                        dbRole = ctx.RbacRoles.Where(r => r.Name.Equals(role.Name, StringComparison.OrdinalIgnoreCase)).SingleOrDefault();
                    }

                    if (dbRole == null)
                    {
                        RbacRole newRole = Assign(role);
                        newRole.Version = 1;
                        dbRole          = ctx.RbacRoles.Add(newRole);
                    }
                    else
                    {
                        dbRole.Name                 = role.Name;
                        dbRole.Description          = role.Description;
                        dbRole.MetaDataRbac         = GetEncryptedString(role.MetaDataRbac);
                        dbRole.MetaDataEntitlements = GetEncryptedString(role.MetaDataEntitlements);
                        dbRole.Version              = role.Version + 1;
                    }

                    ctx.SaveChanges();
                }
            }
            catch (DbEntityValidationException e)
            {
                RaiseError(e);
            }
            catch (DbUpdateException dbe)
            {
                RaiseError(dbe);
            }
            return(Assign(dbRole));
        }
Exemplo n.º 4
0
        public static string GetEFConnectionString()
        {
            var cs = ConfigurationManager.ConnectionStrings["aarbac"];

            if (cs == null)
            {
                RbacException.Raise(Symplus.Rbac.MessagesConfigurations.conn_str_not_found, RbacExceptionCategories.Configuration);
            }

            string entConnection =
                string.Format(
                    "metadata=res://*/DataManager.RbacDbModel.csdl|res://*/DataManager.RbacDbModel.ssdl|res://*/DataManager.RbacDbModel.msl;provider=System.Data.SqlClient;provider connection string=\"{0};MultipleActiveResultSets=True;App=EntityFramework\"",
                    cs.ConnectionString);

            return(entConnection);
        }
Exemplo n.º 5
0
        private Rbac GetRbacFromContext()
        {
            string rbacName = string.Empty;

            try
            {
                rbacName = ((ClaimsIdentity)User.Identity).Claims.FirstOrDefault(x => x.Type == "rbac").Value;
            }
            catch { }

            if (string.IsNullOrEmpty(rbacName))
            {
                RbacException.Raise("Parameter rbac was not passed in the request!", RbacExceptionCategories.Web);
            }

            //Rbac rbac = new Rbac(rbacName);
            //if (rbac == null)
            //    RbacException.Raise(string.Format("Rbac instance with rbac name '{0}' not found!", rbacName));

            return(null);
        }
Exemplo n.º 6
0
        private string RaiseError(DbEntityValidationException e, bool raiseError = true)
        {
            List <string> errors = new List <string>();

            foreach (var eve in e.EntityValidationErrors)
            {
                string s = string.Format("{0} {1}", eve.Entry.Entity.GetType().Name, eve.Entry.State);
                foreach (var ve in eve.ValidationErrors)
                {
                    errors.Add(string.Format("{0} - Property {1}, Error = {2}", s,
                                             ve.PropertyName, ve.ErrorMessage));
                    errors.Add(s);
                }
            }

            string errorMessage = errors.Select(i => i).Aggregate((i, j) => i + Environment.NewLine + j);

            if (raiseError)
            {
                RbacException.Raise(errorMessage, RbacExceptionCategories.Repository);
            }

            return(errorMessage);
        }