Exemplo n.º 1
0
        /**
         *  Main Methode: Reads the operation and assigns the right CRUD operation on the incoming user
         *      @param1 => The method/operation of the CRUD to be executed
         *      @param2 => The incoming user object that is used for the CRUD
         *      @param3 => The CRUD instance with all the CRUD functionality inside
         */
        public static void OperationToCRUD(this string operation, IntraUser user, CRUD crudInstance)
        {
            //Makes an AD/C# user object for ease of use
            var adUser = user.IntraUserObjectToADObject();

            //Check for operation with switch case
            switch (operation.ToUpperInvariant())
            {
            //Creates a new user in the Active Directory DB
            case "CREATE":
                if (crudInstance.CreateUser(user.IntraUserObjectToADObject()))
                {
                    //Check if new user made it into the DB
                    if (crudInstance.IsUserInAD(adUser.CN))
                    {
                        //Get the newly generated GUID from the Attributes
                        user.MetaData.GUID = crudInstance.FindADUser(adUser.CN).ObjectGUID;
                        //Update the UUID with the newly created user
                        Uuid.Update(user);
                    }
                    else
                    {
                        //If user is not found, display an error
                        Console.WriteLine("##################################################");
                        Console.WriteLine($"#      Newly created user cannot be found        #");
                        Console.WriteLine("##################################################");
                    }
                }
                else
                {
                    //Show error if Create got interrupted
                    Console.WriteLine("##################################################");
                    Console.WriteLine($"# CREATING user from Active Directory has FAILED #");
                    Console.WriteLine("##################################################");

                    //Make a XML Error string to log error
                    string error = "<error>" +
                                   "<header>" +
                                   "<code>3003</code>" +
                                   "<origin>AD</origin>" +
                                   "<timestamp>" + DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss%K") + "</timestamp>" +
                                   "</header>" +
                                   "<body>" +
                                   "<objectUUID></objectUUID>" +
                                   "<objectSourceId></objectSourceId>" +
                                   "<objectOrigin>AD</objectOrigin>" +
                                   "<description>The user could not be added to the AD</description>" +
                                   "</body>" +
                                   "</error>";
                    //Send the error on the Logging Queue
                    ProducerGUI.send(error, Severity.logging.ToString());
                }
                break;

            //Deletes an existing user from the Active Directory DB
            case "DELETE":
                if (crudInstance.DeleteUser(adUser.CN))
                {
                    /**
                     *  Because the GUI only has the Container name of all the users
                     *  the DELETE method only comes wrapped around a READ method,
                     *  to first find the user and with that reference => delete the user
                     */
                    user.MetaData.Methode = CRUDMethode.DELETE;
                    //Update the UUID with the deleted user
                    Uuid.Update(user);
                }
                else
                {
                    //Show error if Delete got interrupted
                    Console.WriteLine("##################################################");
                    Console.WriteLine($"# DELETING user from Active Directory has FAILED #");
                    Console.WriteLine("##################################################");
                }
                break;

            //Updates an existing user from the Active Directory DB
            case "UPDATE":
                //Find the old user object based on the same GUID from both users
                var oldUser = crudInstance.FindADUser("objectGUID=" + ConvertGuidToOctetString(user.MetaData.GUID));
                if (crudInstance.UpdateUser(oldUser, user.IntraUserObjectToADObject()))
                {
                    //Update the UUID with the newly updated user
                    Uuid.Update(user);
                }
                else
                {
                    //Show error if Update got interrupted
                    Console.WriteLine("##################################################");
                    Console.WriteLine($"# UPDATING user from Active Directory has FAILED #");
                    Console.WriteLine("##################################################");
                }
                break;

            //Reads all users in Active Directory and wrap it into a List<ADUser>
            case "READ_ALL":
                //Get all users
                ListUsers.List = crudInstance.GetADUsers();
                //Converts List into a XML Array and send it on the Queue of the GUI
                ProducerGUI.send(ObjectToXML(ListUsers.List), Severity.GUI.ToString());     //Make new Producer
                break;

            default:
                Console.WriteLine("##################################################");
                Console.WriteLine($"#       INVALID Operation: {operation} ");
                Console.WriteLine("##################################################");
                break;
            }
        }