Beispiel #1
0
        public IActionResult CreateGroup([FromBody] CreateGroupDetails createGroupDetails)
        {
            var    queryResult      = -1; //Set query result to fail.
            string connectionString = Configuration["ConnectionStrings:DefaultConnectionString"];
            int    result           = -1;

            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                //Create the SQL command and set type to stored procedure.
                SqlCommand command = new SqlCommand("Group_Create", connection);
                command.CommandType = System.Data.CommandType.StoredProcedure;

                //Set the parameters for the command.
                command.Parameters.AddWithValue("@userID", createGroupDetails.UserID);
                command.Parameters.AddWithValue("@groupName", createGroupDetails.GroupName);
                command.Parameters.Add("@result", System.Data.SqlDbType.Int).Direction = System.Data.ParameterDirection.Output;

                connection.Open();

                //Execute the query and store the result
                queryResult = command.ExecuteNonQuery();

                result = (int)command.Parameters["@result"].Value;

                connection.Close();
            }

            if (result == 1)
            {
                //SUCCESS
                return(Ok("Group created."));
            }
            else
            {
                //FAIL
                return(BadRequest("Failed to add group, group name already exists."));
            }
        }
        private static async Task UserAndGroupOps(IdentityClient client, string compartmentId)
        {
            // create a test user
            CreateUserDetails createUserDetails = new CreateUserDetails
            {
                CompartmentId = compartmentId,
                Name          = userName,
                Description   = "testing oci sdk for .NET"
            };
            CreateUserRequest createUserRequest = new CreateUserRequest {
                CreateUserDetails = createUserDetails
            };
            CreateUserResponse createUserResponse = await client.CreateUser(createUserRequest);

            User user = createUserResponse.User;

            logger.Info($"user created, Name : {user.Name} , ID : {user.Id}");

            // create a test group
            CreateGroupDetails createGroupDetails = new CreateGroupDetails
            {
                CompartmentId = compartmentId,
                Name          = "oci-dotnetsdk-testgroup",
                Description   = "testing oci sdk for .NET"
            };
            CreateGroupRequest createGroupRequest = new CreateGroupRequest
            {
                CreateGroupDetails = createGroupDetails
            };
            CreateGroupResponse createGroupResponse = await client.CreateGroup(createGroupRequest);

            logger.Info($"new group created, Name : {createGroupResponse.Group.Name} , Id : {createGroupResponse.Group.Id}");
            Group group = createGroupResponse.Group;

            // add the user to the group
            logger.Info("Adding new user to the new group");
            AddUserToGroupDetails addUserToGroupDetails = new AddUserToGroupDetails
            {
                UserId  = user.Id,
                GroupId = group.Id
            };

            AddUserToGroupRequest addUserToGroupRequest = new AddUserToGroupRequest {
                AddUserToGroupDetails = addUserToGroupDetails
            };
            AddUserToGroupResponse addUserToGroupResponse = await client.AddUserToGroup(addUserToGroupRequest);

            logger.Info($"Added user: {user.Name} to the group: {group.Name}");

            // remove user from the group
            logger.Info($"removing user: {user.Name} from the group: {group.Name}");
            RemoveUserFromGroupRequest removeUserFromGroupRequest = new RemoveUserFromGroupRequest
            {
                UserGroupMembershipId = addUserToGroupResponse.UserGroupMembership.Id
            };
            await client.RemoveUserFromGroup(removeUserFromGroupRequest);

            // delete the user
            logger.Info($"deleting the user: {user.Name}");
            DeleteUserRequest deleteUserRequest = new DeleteUserRequest {
                UserId = user.Id
            };
            await client.DeleteUser(deleteUserRequest);

            // delete the group
            logger.Info($"deleting the group: {group.Name}");
            DeleteGroupRequest deleteGroupRequest = new DeleteGroupRequest {
                GroupId = group.Id
            };
            await client.DeleteGroup(deleteGroupRequest);

            logger.Info("Finished delete user and group");
        }