private IGroup CreateGroupInt( UserManagerData userManagerData, string name, string displayName, ID<IUserOrGroup, Guid>? ownerId, ID<IUserOrGroup, Guid> groupId, bool builtIn, bool automatic, GroupType groupType) { name = name.ToLowerInvariant(); if (groupType >= GroupType.Private) this.ThrowExceptionIfDuplicate(userManagerData, name); UserInt owner = null; if (null != ownerId) owner = userManagerData.GetUser(ownerId.Value); var group = new GroupInt() { name = groupType > GroupType.Personal ? name : groupId.ToString(), id = groupId, owner = owner, builtIn = builtIn, automatic = automatic, type = groupType, displayName = displayName }; if (GroupType.Personal == groupType) group.aliases[owner] = name; userManagerData.groups[groupId] = group; if (groupType != GroupType.Personal) userManagerData.byName[name] = group; var groupObj = this.CreateGroupObject(group); IDirectoryHandler usersDirectory = FileHandlerFactoryLocator.FileSystemResolver.ResolveFile("Users").CastFileHandler<IDirectoryHandler>(); string groupFileName = name + ".group"; if (!automatic) { // Decide where the object goes, for personal groups in the user's directory, for system groups in the users directory IDirectoryHandler groupObjectDestinationDirectory; if (groupType == GroupType.Personal) groupObjectDestinationDirectory = usersDirectory.OpenFile(owner.name).CastFileHandler<IDirectoryHandler>(); else groupObjectDestinationDirectory = usersDirectory; INameValuePairsHandler groupDB; try { groupDB = groupObjectDestinationDirectory.CreateFile(groupFileName, "group", ownerId).FileContainer.CastFileHandler<INameValuePairsHandler>(); ; } catch (DuplicateFile) { throw new UserAlreadyExistsException(name + " already exists"); } IUser ownerObj = null; if (null != owner) ownerObj = this.CreateUserObject(owner); groupObjectDestinationDirectory.SetPermission( ownerObj, groupFileName, new ID<IUserOrGroup, Guid>[] { groupId }, FilePermissionEnum.Read, true, true); // Everyone can read a public group if (GroupType.Public == groupType) usersDirectory.SetPermission( ownerObj, groupFileName, new ID<IUserOrGroup, Guid>[] { FileHandlerFactoryLocator.UserFactory.Everybody.Id }, FilePermissionEnum.Read, true, false); groupDB.Set(ownerObj, "GroupId", groupId.Value.ToString()); } log.Info("Created group: " + name); return groupObj; }
/// <summary> /// Throws a UserAlreadyExistsException if there is a user or group with the same name /// </summary> /// <param name="name"> /// A <see cref="System.String"/> /// </param> private void ThrowExceptionIfDuplicate(UserManagerData userManagerData, string name) { if (userManagerData.byName.ContainsKey(name)) throw new UserAlreadyExistsException("Duplicate user: " + name); }