Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -244,9 +244,9 @@ public IEnumerable<PSADUser> FilterUsers()
public List<PSADObject> ListUserGroups(string principal)
{
List<PSADObject> result = new List<PSADObject>();
Guid objectId = GetObjectId(new ADObjectFilterOptions { UPN = principal });
PSADObject user = GetADObject(new ADObjectFilterOptions { Id = objectId.ToString() });
var groupsIds = GraphClient.Users.GetMemberGroups(objectId.ToString(), new UserGetMemberGroupsParameters());
string objectId = GetObjectIdAsString(new ADObjectFilterOptions { UPN = principal });
PSADObject user = GetADObject(new ADObjectFilterOptions { Id = objectId });
var groupsIds = GraphClient.Users.GetMemberGroups(objectId, new UserGetMemberGroupsParameters());
var groupsResult = GraphClient.Objects.GetObjectsByObjectIds(new GetObjectsParameters { ObjectIds = groupsIds.ToList() });
result.AddRange(groupsResult.Select(g => g.ToPSADGroup()));

Expand Down Expand Up @@ -393,7 +393,50 @@ public Guid GetObjectId(ADObjectFilterOptions options)

return principalId;
}
public string GetObjectIdAsString(ADObjectFilterOptions options)
{
Guid principalId;
if (options != null && options.Id != null
&& Guid.TryParse(options.Id, out principalId))
{
// do nothing, we have parsed the guid
}
else
{
PSADObject adObj = GetADObject(options);

if (adObj == null)
{
throw new KeyNotFoundException("The provided information does not map to an AD object id.");
}

principalId = adObj.Id;
}

return principalId.ToString();
}

public string GetAdfsObjectId(ADObjectFilterOptions options)
{
string principalId = null;
if (options != null && options.Id != null)
{
// do nothing, we have parsed the guid
}
else
{
PSADObject adObj = GetADObject(options);

if (adObj == null)
{
throw new KeyNotFoundException("The provided information does not map to an AD object id.");
}

principalId = adObj.AdfsId;
}

return principalId;
}
public void UpdateApplication(Guid appObjectId, ApplicationUpdateParameters parameters)
{
GraphClient.Applications.Patch(appObjectId.ToString(), parameters);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,30 @@ internal static class ActiveDirectoryClientExtensions
{
public static PSADObject ToPSADObject(this User user)
{
return new PSADObject()
{
DisplayName = user.DisplayName,
Id = new Guid(user.ObjectId)
};
var adObj = new PSADObject() { DisplayName = user.DisplayName };
return AssignObjectId(adObj, user.ObjectId);
}

public static PSADObject ToPSADObject(this ADGroup group)
{
return new PSADObject()
var adObj = new PSADObject() { DisplayName = group.DisplayName };
return AssignObjectId(adObj, group.ObjectId);
}

public static PSADObject AssignObjectId(PSADObject adObj, string objectId)
{
Guid objectIdGuid;

if (Guid.TryParse(objectId, out objectIdGuid))
{
DisplayName = group.DisplayName,
Id = new Guid(group.ObjectId)
};
adObj.Id = objectIdGuid;
}
else
{
adObj.AdfsId = objectId;
}

return adObj;
}

public static PSADObject ToPSADObject(this AADObject obj)
Expand All @@ -44,89 +54,95 @@ public static PSADObject ToPSADObject(this AADObject obj)

if (obj.ObjectType == typeof(User).Name)
{
return new PSADUser()
var adUser = new PSADUser()
{
DisplayName = obj.DisplayName,
Id = new Guid(obj.ObjectId),
Type = obj.ObjectType,
UserPrincipalName = obj.UserPrincipalName
};

return AssignObjectId(adUser, obj.ObjectId);
}
else if (obj.ObjectType == "Group")
{
return new PSADGroup()
var adGroup = new PSADGroup()
{
DisplayName = obj.DisplayName,
Type = obj.ObjectType,
Id = new Guid(obj.ObjectId),
SecurityEnabled = obj.SecurityEnabled,
MailNickname = obj.Mail
};

return AssignObjectId(adGroup, obj.ObjectId);
}
else if (obj.ObjectType == typeof(ServicePrincipal).Name)
{
return new PSADServicePrincipal()
var adSp = new PSADServicePrincipal()
{
DisplayName = obj.DisplayName,
Id = new Guid(obj.ObjectId),
Type = obj.ObjectType,
ServicePrincipalNames = obj.ServicePrincipalNames.ToArray()
};

return AssignObjectId(adSp, obj.ObjectId);
}
else
{
return new PSADObject()
var adObj = new PSADObject()
{
DisplayName = obj.DisplayName,
Id = new Guid(obj.ObjectId),
Type = obj.ObjectType
};

return AssignObjectId(adObj, obj.ObjectId);
}
}

public static PSADObject ToPSADGroup(this AADObject obj)
{
return new PSADObject()
var adObj = new PSADObject()
{
DisplayName = obj.DisplayName,
Id = new Guid(obj.ObjectId)
};

return AssignObjectId(adObj, obj.ObjectId);
}

public static PSADUser ToPSADUser(this User user)
{
return new PSADUser()
var adUser = new PSADUser()
{
DisplayName = user.DisplayName,
Id = new Guid(user.ObjectId),
UserPrincipalName = user.UserPrincipalName,
Type = user.ObjectType
};

return (PSADUser) AssignObjectId(adUser, user.ObjectId);
}

public static PSADGroup ToPSADGroup(this ADGroup group)
{
return new PSADGroup()
var adGroup = new PSADGroup()
{
DisplayName = group.DisplayName,
Id = new Guid(group.ObjectId),
SecurityEnabled = group.SecurityEnabled,
Type = group.ObjectType,
MailNickname = group.Mail
};

return (PSADGroup) AssignObjectId(adGroup, group.ObjectId);
}

public static PSADServicePrincipal ToPSADServicePrincipal(this ServicePrincipal servicePrincipal)
{
return new PSADServicePrincipal()
var adSp = new PSADServicePrincipal()
{
DisplayName = servicePrincipal.DisplayName,
Id = new Guid(servicePrincipal.ObjectId),
ApplicationId = Guid.Parse(servicePrincipal.AppId),
ServicePrincipalNames = servicePrincipal.ServicePrincipalNames.ToArray(),
Type = servicePrincipal.ObjectType
};

return (PSADServicePrincipal) AssignObjectId(adSp, servicePrincipal.ObjectId);
}

public static PSADApplication ToPSADApplication(this Application application)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public class PSADObject

public Guid Id { get; set; }

public string AdfsId { get; set; }

public string Type { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -127,15 +127,15 @@ public IEnumerable<PSRoleDefinition> FilterRoleDefinitionsByCustom(string scope,
/// <returns>The created role assignment object</returns>
public PSRoleAssignment CreateRoleAssignment(FilterRoleAssignmentsOptions parameters, Guid roleAssignmentId = default(Guid))
{
Guid principalId = ActiveDirectoryClient.GetObjectId(parameters.ADObjectFilter);
string principalId = ActiveDirectoryClient.GetObjectIdAsString(parameters.ADObjectFilter);
roleAssignmentId = roleAssignmentId == default(Guid) ? Guid.NewGuid() : roleAssignmentId;
string scope = parameters.Scope;
string roleDefinitionId = !string.IsNullOrEmpty(parameters.RoleDefinitionName)
? AuthorizationHelper.ConstructFullyQualifiedRoleDefinitionIdFromScopeAndIdAsGuid(scope, GetSingleRoleDefinitionByName(parameters.RoleDefinitionName, scope).Id)
: AuthorizationHelper.ConstructFullyQualifiedRoleDefinitionIdFromScopeAndIdAsGuid(scope, parameters.RoleDefinitionId);
var createParameters = new RoleAssignmentCreateParameters
{
PrincipalId = principalId.ToString(),
PrincipalId = principalId,
RoleDefinitionId = roleDefinitionId,
CanDelegate = parameters.CanDelegate
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ private static IEnumerable<PSRoleAssignment> ToPSRoleAssignments(this IEnumerabl
{
assignment.RoleDefinitionId = assignment.RoleDefinitionId.GuidFromFullyQualifiedId();
PSADObject adObject = adObjects.SingleOrDefault(o => o.Id == Guid.Parse(assignment.PrincipalId)) ??
new PSADObject() { Id = Guid.Parse(assignment.PrincipalId) };
new PSADObject() { Id = Guid.Parse(assignment.PrincipalId) };
PSRoleDefinition roleDefinition = roleDefinitions.SingleOrDefault(r => r.Id == assignment.RoleDefinitionId) ??
new PSRoleDefinition() { Id = assignment.RoleDefinitionId };
bool delegationFlag = assignment.CanDelegate.HasValue ? (bool)assignment.CanDelegate : false;
Expand Down