Exemple #1
0
        public Models.UserResponse CreateUser(Models.UserRequest value, long organization)
        {
            Models.UserResponse response = new Models.UserResponse();

            try
            {
                //SQL Statement
                var sqlString = "INSERT INTO users (id, first_name, description, organization, email_address, password, security_token, role, user_group, agreed_to_terms_and_policies, enabled, last_name) " +
                                "VALUES (@id, @first_name, @description, @organization, @email_address, @password, @security_token, @role, @user_group, @agreed_to_terms_and_policies, @enabled, @last_name)";

                //Create UNIX Timestamp
                var utcDateTime  = DateTime.UtcNow;
                var dto          = new DateTimeOffset(utcDateTime);
                var unixDateTime = dto.ToUnixTimeMilliseconds();

                var  random        = new Random();
                int  rnd           = random.Next(1000000000, 2000000000);
                long securityToken = unixDateTime - rnd;

                using (var connection = new NpgsqlConnection(connectionString))
                {
                    connection.Open();

                    using (var command = new NpgsqlCommand(sqlString, connection))
                    {
                        command.Parameters.AddWithValue("@id", NpgsqlTypes.NpgsqlDbType.Bigint, unixDateTime);
                        command.Parameters.AddWithValue("@first_name", NpgsqlTypes.NpgsqlDbType.Varchar, value.FirstName);
                        command.Parameters.AddWithValue("@last_name", NpgsqlTypes.NpgsqlDbType.Varchar, value.LastName);
                        command.Parameters.AddWithValue("@description", NpgsqlTypes.NpgsqlDbType.Varchar, value.UserDescription);
                        command.Parameters.AddWithValue("@organization", NpgsqlTypes.NpgsqlDbType.Bigint, organization);
                        command.Parameters.AddWithValue("@email_address", NpgsqlTypes.NpgsqlDbType.Varchar, value.UserEmailAddress);
                        command.Parameters.AddWithValue("@password", NpgsqlTypes.NpgsqlDbType.Varchar, value.UserPassword);
                        command.Parameters.AddWithValue("@security_token", NpgsqlTypes.NpgsqlDbType.Bigint, securityToken);
                        command.Parameters.AddWithValue("@role", NpgsqlTypes.NpgsqlDbType.Bigint, value.Role);
                        command.Parameters.AddWithValue("@user_group", NpgsqlTypes.NpgsqlDbType.Bigint, value.UserGroup);
                        command.Parameters.AddWithValue("@agreed_to_terms_and_policies", NpgsqlTypes.NpgsqlDbType.Bigint, value.AgreeToTermsAndPolicies);
                        command.Parameters.AddWithValue("@enabled", NpgsqlTypes.NpgsqlDbType.Bigint, value.Enabled);
                        command.Prepare();
                        command.ExecuteNonQuery();

                        //Log Success
                        response.Status        = "success";
                        response.Message       = "user created";
                        response.Id            = unixDateTime;
                        response.SecurityToken = securityToken;
                        return(response);
                    }
                }
            }
            catch (Exception ex)
            {
                //Log Exception
                //_logger.LogError(ex, "user creation failed");
                response.Status        = "error";
                response.Message       = "user creation failed";
                response.Id            = 0;
                response.SecurityToken = 0;
                return(response);
            }
        }
        public Models.UserResponse CreateUser(Models.UserRequest value, Guid organization)
        {
            Models.UserResponse response = new Models.UserResponse();

            try
            {
                //SQL Statement
                var sqlString = "INSERT INTO users (id, first_name, last_name, description, email_address, password, security_token, organization, primary_user, role, created, created_by) " +
                                "VALUES (@id, @first_name, @last_name, @description, @email_address, @password, @security_token, @organization, @primary_user, @role, @created, @created_by)";

                //Create a new User Id UUID
                Guid userIdGuid = Guid.NewGuid();
                //Create a new Security Token UUID
                Guid securityTokenGuid = Guid.NewGuid();

                using (var connection = new NpgsqlConnection(connectionString))
                {
                    connection.Open();

                    using (var command = new NpgsqlCommand(sqlString, connection))
                    {
                        command.Parameters.AddWithValue("@id", NpgsqlTypes.NpgsqlDbType.Uuid, userIdGuid);
                        command.Parameters.AddWithValue("@first_name", NpgsqlTypes.NpgsqlDbType.Text, value.FirstName);
                        command.Parameters.AddWithValue("@last_name", NpgsqlTypes.NpgsqlDbType.Text, value.LastName);
                        command.Parameters.AddWithValue("@description", NpgsqlTypes.NpgsqlDbType.Text, value.Description);
                        command.Parameters.AddWithValue("@email_address", NpgsqlTypes.NpgsqlDbType.Text, value.EmailAddress);
                        command.Parameters.AddWithValue("@password", NpgsqlTypes.NpgsqlDbType.Text, value.Password);
                        command.Parameters.AddWithValue("@security_token", NpgsqlTypes.NpgsqlDbType.Uuid, securityTokenGuid);
                        command.Parameters.AddWithValue("@organization", NpgsqlTypes.NpgsqlDbType.Uuid, organization);
                        command.Parameters.AddWithValue("@primary_user", NpgsqlTypes.NpgsqlDbType.Bigint, 0);
                        command.Parameters.AddWithValue("@role", NpgsqlTypes.NpgsqlDbType.Bigint, value.Role);
                        command.Parameters.AddWithValue("@created", NpgsqlTypes.NpgsqlDbType.TimestampTz, DateTime.UtcNow);
                        command.Parameters.AddWithValue("@created_by", NpgsqlTypes.NpgsqlDbType.Uuid, value.CreatedBy);
                        command.Prepare();
                        command.ExecuteNonQuery();

                        //Log Success
                        response.Status        = "success";
                        response.Message       = "user created";
                        response.Id            = userIdGuid;
                        response.SecurityToken = securityTokenGuid;
                        return(response);
                    }
                }
            }
            catch (Exception ex)
            {
                //Log Exception
                _logger.LogError(ex, "user creation failed");
                response.Status        = "error";
                response.Message       = "user creation failed";
                response.Id            = errorGuid;
                response.SecurityToken = errorGuid;
                return(response);
            }
        }
Exemple #3
0
        private async void btnCreateUser_Click(object sender, EventArgs e)
        {
            try
            {
                if (txtFirstName.Text != "" && txtLastName.Text != "" && txtDescription.Text != "" && txtEmailAddress.Text != "" && txtPassword.Text != "" && txtPasswordRetype.Text != "")
                {
                    if (txtPassword.Text.Trim() == txtPasswordRetype.Text.Trim())
                    {
                        //Capture Values
                        Models.UserRequest userRequest = new Models.UserRequest();
                        userRequest.FirstName    = txtFirstName.Text.Trim();
                        userRequest.LastName     = txtLastName.Text.Trim();
                        userRequest.Description  = txtDescription.Text.Trim();
                        userRequest.EmailAddress = txtEmailAddress.Text.Trim();
                        userRequest.Password     = txtPassword.Text.Trim();
                        userRequest.Role         = (comboBoxRole.SelectedItem as Models.Role).Id;
                        userRequest.CreatedBy    = Program.identity;

                        //Create JSON Document
                        var jsonString = JsonConvert.SerializeObject(userRequest);

                        //Clear Values
                        txtFirstName.Clear();
                        txtLastName.Clear();
                        txtDescription.Clear();
                        txtEmailAddress.Clear();
                        txtPassword.Clear();
                        txtPasswordRetype.Clear();

                        string credentials = Program.identity.ToString() + "." + Program.securityToken.ToString();

                        //Send Data
                        ClientSDK clientSDK  = new ClientSDK();
                        string    uriString  = Program.serverURL + "/User";
                        var       jsonResult = await clientSDK.Create(uriString, jsonString, credentials);

                        var objectResult = JsonConvert.DeserializeObject <Models.UserResponse>(jsonResult);

                        //Add to User List
                        ListViewItem listViewItem = new ListViewItem(objectResult.Id.ToString());
                        listViewItem.SubItems.Add(userRequest.FirstName);
                        listViewItem.SubItems.Add(userRequest.LastName);
                        listViewItem.SubItems.Add(userRequest.Description);

                        if (userRequest.Role == 1)
                        {
                            listViewItem.SubItems.Add("Writer");
                        }
                        else
                        {
                            listViewItem.SubItems.Add("Reader");
                        }

                        listViewUsers.Items.Add(listViewItem);
                    }
                    else
                    {
                        MessageBox.Show("The Password fields must match.", "Information");
                    }
                }
                else
                {
                    MessageBox.Show("All fields must be properly filled-in.", "Information");
                }
            }
            catch (Exception ex)
            {
                if (ex.Message == "404")
                {
                    //No data returned
                }
                else if (ex.Message == "401")
                {
                    MessageBox.Show("The email address or password you entered is either incorrect or this user doesn't exist in the system", "Error");
                }
                else if (ex.Message == "An error occurred while sending the request.")
                {
                    MessageBox.Show("The Moab Platform is unreachable.", "Network Error");
                }
                else
                {
                    MessageBox.Show(ex.Message);
                }
            }
            finally
            {
            }
        }