Exemple #1
0
        private async Task CreateRoleAssignmentsAsync(HttpClient httpClient, IList <RoleAssignmentDescription> roleAssignmentDescriptions, Guid spaceId)
        {
            if (spaceId == Guid.Empty)
            {
                throw new ArgumentException($"RoleAssignments must have a {nameof( spaceId )}");
            }

            string spacePath = await SpaceHelpers.GetSpaceFullPathAsync(httpClient, spaceId);

            foreach (RoleAssignmentDescription roleAssignmentDescription in roleAssignmentDescriptions)
            {
                string objectId;
                switch (roleAssignmentDescription.objectIdType)
                {
                case RoleAssignment.ObjectIdTypes.UserDefinedFunctionId:
                    objectId = (await UserDefinedFunctionHelpers.FindUserDefinedFunctionAsync(httpClient,
                                                                                              roleAssignmentDescription.objectName, spaceId))?.Id;
                    break;

                default:
                    throw new ArgumentOutOfRangeException(
                              $"{nameof( RoleAssignment )} with {nameof( RoleAssignmentDescription.objectName )} must" +
                              $" have a known {nameof( RoleAssignmentDescription.objectIdType )}" +
                              $" but instead has {roleAssignmentDescription.objectIdType}");
                }

                if (objectId != null)
                {
                    var roleAssignment = roleAssignmentDescription.ToDigitalTwins(objectId, spacePath);

                    var existingRoleAssignment = await roleAssignment.GetUniqueRoleAssignmentAsync(httpClient);

                    if (existingRoleAssignment == null)
                    {
                        await roleAssignment.CreateRoleAssignmentAsync(httpClient, JsonSerializerSettings);
                    }
                    else
                    {
                        Console.WriteLine($"{nameof( RoleAssignment )} already exists, so skipping creation.");
                    }
                }
            }
        }
Exemple #2
0
        private async Task CreateUserRoleAssignmentsAsync(HttpClient httpClient, IList <string> users, Guid spaceId,
                                                          UserAadObjectIdsDescription userAadObjectIds)
        {
            if (spaceId == Guid.Empty)
            {
                throw new ArgumentException($"User Role Assignment must have a {nameof( spaceId )}");
            }

            foreach (string user in users)
            {
                if (userAadObjectIds.TryGetValue(user, out string oid))
                {
                    string spacePath = await SpaceHelpers.GetSpaceFullPathAsync(httpClient, spaceId);

                    if (string.IsNullOrWhiteSpace(spacePath))
                    {
                        Console.WriteLine($"Unable to get the full path for the current space. Cannot create a role assignment. (Space Id: {spaceId})");
                        continue;
                    }

                    var roleAssignment = new RoleAssignment
                    {
                        RoleId       = RoleAssignment.RoleIds.User,
                        ObjectId     = oid,
                        ObjectIdType = RoleAssignment.ObjectIdTypes.UserId,
                        TenantId     = Tenant,
                        Path         = spacePath
                    };

                    var existingRoleAssignment = await roleAssignment.GetUniqueRoleAssignmentAsync(httpClient);

                    if (existingRoleAssignment == null)
                    {
                        await roleAssignment.CreateRoleAssignmentAsync(httpClient, JsonSerializerSettings);
                    }
                    else
                    {
                        Console.WriteLine($"{nameof( RoleAssignment )} already exists, so skipping creation.");
                    }
                }
            }
        }