示例#1
0
        private static void CreateSysAdminUser(AuthorityGroup adminGroup, SetupCommandLine cmdLine, IPersistenceContext context, TextWriter log)
        {
            try
            {
                // create the sa user, if doesn't already exist
                IUserBroker userBroker = context.GetBroker<IUserBroker>();
                UserSearchCriteria where = new UserSearchCriteria();
				where.UserName.EqualTo(cmdLine.SysAdminUserName);
                userBroker.FindOne(where);

				log.WriteLine(string.Format("User '{0}' already exists.", cmdLine.SysAdminUserName));
            }
            catch (EntityNotFoundException)
            {
                HashedSet<AuthorityGroup> groups = new HashedSet<AuthorityGroup>
                                                       {
                                                           adminGroup
                                                       };

                // create sa user using initial password, set to expire never
                User saUser = User.CreateNewUser(
					new UserInfo(cmdLine.SysAdminUserName, cmdLine.SysAdminDisplayName, null, null, null),
					Password.CreatePassword(cmdLine.SysAdminInitialPassword, null),
                    groups);
                context.Lock(saUser, DirtyState.New);
            }
        }
		internal AuthorityGroupSummary CreateAuthorityGroupSummary(AuthorityGroup authorityGroup)
		{
			return new AuthorityGroupSummary(
				authorityGroup.GetRef(),
				authorityGroup.Name,
				authorityGroup.Description,
				authorityGroup.BuiltIn,
				authorityGroup.DataGroup);
		}
        internal AuthorityGroupDetail CreateAuthorityGroupDetail(AuthorityGroup authorityGroup)
        {

            AuthorityTokenAssembler assembler = new AuthorityTokenAssembler();
        	List<AuthorityTokenSummary> tokens = CollectionUtils.Map<AuthorityToken, AuthorityTokenSummary>(
        		authorityGroup.AuthorityTokens,
        		assembler.GetAuthorityTokenSummary);

            return new AuthorityGroupDetail(
                authorityGroup.GetRef(),
                authorityGroup.Name,
                authorityGroup.Description,
                authorityGroup.DataGroup,
                tokens);
        }
示例#4
0
		/// <summary>
		/// Import authority groups.
		/// </summary>
		/// <remarks>
		/// Creates any authority groups that do not already exist.
		/// This method performs an additive import.  It will never remove an existing authority group or
		/// remove authority tokens from an existing group.
		/// </remarks>
		/// <param name="groupDefs"></param>
		/// <param name="context"></param>
		public IList<AuthorityGroup> Import(IEnumerable<AuthorityGroupDefinition> groupDefs, IUpdateContext context)
        {
            // first load all the existing tokens into memory
            // there should not be that many tokens ( < 500), so this should not be a problem
            IAuthorityTokenBroker tokenBroker = context.GetBroker<IAuthorityTokenBroker>();
            IList<AuthorityToken> existingTokens = tokenBroker.FindAll();

            // load existing groups
            IAuthorityGroupBroker groupBroker = context.GetBroker<IAuthorityGroupBroker>();
            IList<AuthorityGroup> existingGroups = groupBroker.FindAll();

            foreach (AuthorityGroupDefinition groupDef in groupDefs)
            {
                AuthorityGroup group = CollectionUtils.SelectFirst(existingGroups,
                                                                   g => g.Name == groupDef.Name);

                // if group does not exist, create it
                if (group == null)
                {
                    group = new AuthorityGroup
                                {
                                    Name = groupDef.Name,
                                    Description = groupDef.Description,
                                    DataGroup = groupDef.DataGroup
                                };
                    context.Lock(group, DirtyState.New);
                    existingGroups.Add(group);
                }

                // process all token nodes contained in group
                foreach (string tokenName in groupDef.Tokens)
                {
                    AuthorityToken token = CollectionUtils.SelectFirst(existingTokens,
                                                                       t => t.Name == tokenName);

                    // ignore non-existent tokens
                    if (token == null)
                        continue;

                    // add the token to the group
                    group.AuthorityTokens.Add(token);
                }
            }

            return existingGroups;
        }
        internal void UpdateAuthorityGroup(AuthorityGroup authorityGroup, AuthorityGroupDetail detail, IPersistenceContext persistenceContext)
        {
            authorityGroup.Name = detail.Name;
            authorityGroup.Description = detail.Description;
            authorityGroup.DataGroup = detail.DataGroup;
			authorityGroup.AuthorityTokens.Clear();

			if (detail.AuthorityTokens.Count > 0)
			{
				// process authority tokens
				List<string> tokenNames = CollectionUtils.Map<AuthorityTokenSummary, string>(
					detail.AuthorityTokens,
					token => token.Name);

				AuthorityTokenSearchCriteria where = new AuthorityTokenSearchCriteria();
				where.Name.In(tokenNames);
				IList<AuthorityToken> authTokens = persistenceContext.GetBroker<IAuthorityTokenBroker>().Find(where);

				authorityGroup.AuthorityTokens.AddAll(authTokens);
			}
        }
示例#6
0
		public AddAuthorityGroupResponse AddAuthorityGroup(AddAuthorityGroupRequest request)
		{
			// create new group
			var authorityGroup = new AuthorityGroup();

			// set properties from request
			var assembler = new AuthorityGroupAssembler();
			assembler.UpdateAuthorityGroup(authorityGroup, request.AuthorityGroupDetail, PersistenceContext);

			// save
			PersistenceContext.Lock(authorityGroup, DirtyState.New);
			PersistenceContext.SynchState();

			return new AddAuthorityGroupResponse(assembler.CreateAuthorityGroupSummary(authorityGroup));
		}
示例#7
0
		public int GetUserCountForGroup(AuthorityGroup group)
		{
			var q = new HqlQuery("select count(elements(g.Users)) from AuthorityGroup g");
			q.Conditions.Add(new HqlCondition("g = ?", group));
			return (int)ExecuteHqlUnique<long>(q);
		}
		public AddAuthorityGroupResponse AddAuthorityGroup(AddAuthorityGroupRequest request)
		{
			Platform.CheckForNullReference(request, "request");
			Platform.CheckMemberIsSet(request.AuthorityGroupDetail, "AuthorityGroupDetail");

			if (request.AuthorityGroupDetail.BuiltIn)
				throw new RequestValidationException(SR.MessageCannotManageBuiltInAuthorityGroups);

			// create new group
			var authorityGroup = new AuthorityGroup();

			// set properties from request
			var assembler = new AuthorityGroupAssembler();
			assembler.UpdateAuthorityGroup(authorityGroup, request.AuthorityGroupDetail, PersistenceContext);

			// save
			PersistenceContext.Lock(authorityGroup, DirtyState.New);
			PersistenceContext.SynchState();

			return new AddAuthorityGroupResponse(assembler.CreateAuthorityGroupSummary(authorityGroup));
		}
示例#9
0
文件: UserTests.cs 项目: nhannd/Xian
		public void Test_CreateNewUser_WithExplicitPasswordAndGroups()
		{
			DateTime now = DateTime.Now;
			DateTime tomorrow = now + TimeSpan.FromDays(1);
			UserInfo userInfo = new UserInfo(
				"foo",
				"Mr. Foo",
                "[email protected]",                
				null,
				null);

			AuthorityGroup group1 = new AuthorityGroup();
			AuthorityGroup group2 = new AuthorityGroup();
			HashedSet<AuthorityGroup> groups = new HashedSet<AuthorityGroup>();
			groups.Add(group1);
			groups.Add(group2);

			User user = User.CreateNewUser(userInfo, Password.CreatePassword(DefaultPassword, tomorrow), groups);

			Assert.AreEqual(userInfo.UserName, user.UserName);
			Assert.AreEqual(userInfo.DisplayName, user.DisplayName);
			Assert.AreEqual(true, user.Enabled);	// enabled by default
			Assert.IsTrue(RoughlyEqual(now, user.CreationTime));
			Assert.IsNull(user.LastLoginTime);	// never logged in

			// password
			Assert.IsNotNull(user.Password);
			Assert.IsTrue(user.Password.Verify(DefaultPassword));	// initial password should work

			// password expiry time
			Assert.IsTrue(RoughlyEqual(tomorrow, user.Password.ExpiryTime));

			// groups
			Assert.AreEqual(2, user.AuthorityGroups.Count);
			Assert.IsTrue(user.AuthorityGroups.ContainsAll(groups));
		}