private async Task <Guid> EnsureRoleAsync(RoleSpecification roleDef, Guid tenantId)
        {
            var existingRole = await _roleService.GetRoleInTenantByNameAsync(roleDef.Name, tenantId);

            if (existingRole == null)
            {
                var permValues      = (from p in roleDef.Permissions select p.Value).ToArray();
                var applicationRole = new ApplicationRole(roleDef.Name, roleDef.Description)
                {
                    Id        = roleDef.Id,
                    TenantId  = tenantId,
                    CreatedBy = nameof(AuthorityDbInitializer),
                    UpdatedBy = nameof(AuthorityDbInitializer)
                };

                await _roleService.CreateRoleAsync(applicationRole, permValues);

                return(applicationRole.Id);
            }
            else
            {
                if (existingRole.Id != roleDef.Id)
                {
                    throw new Exception("Logic exception, the existing role should have the id specified in the RoleDef!");
                }
                return(existingRole.Id);
            }
        }
Exemple #2
0
        public BugTrackingResponse <BusinessObjects.AspNetRole> Save(BusinessObjects.AspNetRole role)
        {
            Logger.TraceMethodStart(ClassName, "Save");

            var response = new BugTrackingResponse <BusinessObjects.AspNetRole>();


            using (var db = new EntityModel.BugTrackingEntities())
            {
                using (var transaction = db.Database.BeginTransaction())
                {
                    try
                    {
                        var validator = new RoleSpecification();
                        var result    = validator.Validate(role);
                        var failures  = result.Errors;

                        response.ValidationResult = result;

                        if (result.IsValid)
                        {
                            role.Id           = Guid.NewGuid().ToString();
                            role.LastModified = DateTime.Now;
                            role.Active       = true;
                            var newrole = MapperAllLevels.Map <BusinessObjects.AspNetRole, EntityModel.AspNetRole>(role);
                            db.AspNetRoles.Add(newrole);
                            db.SaveChanges();
                            transaction.Commit();
                        }

                        else
                        {
                            transaction.Rollback();
                            Logger.TraceErrorFormat("Error while Saving {0}", response.ValidationResult.Errors);
                        }

                        return(response);
                    }

                    catch (Exception ex)
                    {
                        Logger.TraceError("Exception: ", ex);
                        transaction.Rollback();
                        throw;
                    }

                    finally
                    {
                        Logger.TraceMethodEnd(ClassName, "Save");
                    }
                }
            }
        }