public void CreateRoles() { if (foundAudience == null) { CreateAudiences(); } /* * create test roles */ foundRole = _uow.Roles.Get(QueryExpressionFactory.GetQueryExpression <E_Role>() .Where(x => x.Name == TestDefaultConstants.RoleName).ToLambda()) .SingleOrDefault(); if (foundRole == null) { foundRole = _uow.Roles.Create( _map.Map <E_Role>(new RoleV1() { AudienceId = foundAudience.Id, Name = TestDefaultConstants.RoleName, IsEnabled = true, IsDeletable = true, })); _uow.Commit(); } }
public AudienceAddRoleCommand() { _conf = (IConfiguration) new ConfigurationBuilder() .AddJsonFile("clisettings.json", optional: false, reloadOnChange: true) .Build(); _map = new MapperConfiguration(x => x.AddProfile <AutoMapperProfile_EF6>()) .CreateMapper(); var env = new ContextService(InstanceContext.DeployedOrLocal); _uow = new UnitOfWork(_conf["Databases:IdentityEntities_EF6"], env); _service = new AdminService(_conf) { Grant = new ResourceOwnerGrantV2(_conf) }; IsCommand("audience-add-role", "Add role to audience"); HasRequiredOption("a|audience=", "Enter existing audience", arg => { if (string.IsNullOrEmpty(arg)) { throw new ConsoleHelpAsException($" *** No audience given ***"); } _audience = _uow.Audiences.Get(QueryExpressionFactory.GetQueryExpression <E_Audience>() .Where(x => x.Name == arg).ToLambda()) .SingleOrDefault(); if (_audience == null) { throw new ConsoleHelpAsException($" *** No audience '{arg}' ***"); } }); HasRequiredOption("r|role=", "Enter existing role", arg => { if (string.IsNullOrEmpty(arg)) { throw new ConsoleHelpAsException($" *** No role given ***"); } _role = _uow.Roles.Get(QueryExpressionFactory.GetQueryExpression <E_Role>() .Where(x => x.Name == arg).ToLambda()) .SingleOrDefault(); if (_role == null) { throw new ConsoleHelpAsException($" *** No role '{arg}' ***"); } }); }
public JsonResult SaveNewUser(ApplicationUser model, int roleId) { E_Role currentRole = (E_Role)Enum.Parse(typeof(E_Role), ApplicationRole.GetById(roleId).Name); bool isAdded = ApplicationUser.AddNew(model.Email, model.UserName, model.Password, currentRole); if (isAdded) { return(Json(true, JsonRequestBehavior.AllowGet)); } return(Json(false, JsonRequestBehavior.AllowGet)); }
public static bool AddNew(string email, string userName, string password, E_Role role) { SinaicspDataModelContainer _context = new Sinaicsp_API.SinaicspDataModelContainer(); if (_context.ApplicationUsers.ToList().Where(a => a.Email.ToLower() == email.ToLower()).Count() == 0 && _context.ApplicationUsers.ToList().Where(a => a.UserName.ToLower() == userName.ToLower()).Count() == 0) { ApplicationUser _item = new Sinaicsp_API.ApplicationUser(); _item.Email = email; _item.UserName = userName; _item.Password = password; _item.CreationDate = DateTime.Now; ApplicationRole _currentRole = ApplicationRole.GetByRole(role); UserRole _userRole = new Sinaicsp_API.UserRole() { ApplicationUser = _item, ApplicationRoleId = _currentRole.Id }; _item.UserRoles.Add(_userRole); _context.ApplicationUsers.Add(_item); _context.SaveChanges(); return(true); } return(false); }
public RoleEditCommand() { _conf = (IConfiguration) new ConfigurationBuilder() .AddJsonFile("clisettings.json", optional: false, reloadOnChange: true) .Build(); _map = new MapperConfiguration(x => x.AddProfile <AutoMapperProfile_EF6>()) .CreateMapper(); var env = new ContextService(InstanceContext.DeployedOrLocal); _uow = new UnitOfWork(_conf["Databases:IdentityEntities_EF6"], env); _service = new AdminService(_conf) { Grant = new ResourceOwnerGrantV2(_conf) }; IsCommand("role-edit", "Edit role"); HasRequiredOption("r|role=", "Enter existing role", arg => { if (string.IsNullOrEmpty(arg)) { throw new ConsoleHelpAsException($" *** No role given ***"); } _role = _uow.Roles.Get(QueryExpressionFactory.GetQueryExpression <E_Role>() .Where(x => x.Name == arg).ToLambda(), new List <Expression <Func <E_Role, object> > >() { x => x.AudienceRoles, x => x.RoleClaims, x => x.UserRoles, }) .SingleOrDefault(); if (_role == null) { throw new ConsoleHelpAsException($" *** No role '{arg}' ***"); } }); HasOption("n|name=", "Enter name", arg => { if (string.IsNullOrEmpty(arg)) { throw new ConsoleHelpAsException($" *** No name given ***"); } _role.Name = arg; }); HasOption("d|description=", "Enter description", arg => { if (string.IsNullOrEmpty(arg)) { throw new ConsoleHelpAsException($" *** No description given ***"); } _role.Description = arg; }); HasOption("E|enabled=", "Is user enabled", arg => { _isEnabled = bool.Parse(arg); }); HasOption("D|deletable=", "Is user deletable", arg => { _isDeletable = bool.Parse(arg); }); }
public static ApplicationRole GetByRole(E_Role role) { SinaicspDataModelContainer _context = new Sinaicsp_API.SinaicspDataModelContainer(); return(_context.ApplicationRoles.ToList().FirstOrDefault(a => a.Name == role.ToString())); }