コード例 #1
0
ファイル: ViewUsers.cs プロジェクト: antseggs/PamDesktop
        private void btnDelete_Click(object sender, EventArgs e)
        {
            // Find selected User
            UserRemoveOrEdit current = new UserRemoveOrEdit();

            current.SessionKey        = information.Token;
            current.UserId            = userList[lstUsers.SelectedIndex].UserId;
            current.PermissionLevelId = userList[lstUsers.SelectedIndex].PermissionLevelId;
            current.FirstName         = userList[lstUsers.SelectedIndex].FirstName;
            current.Surname           = userList[lstUsers.SelectedIndex].Surname;
            current.JobTitle          = userList[lstUsers.SelectedIndex].JobTitle;
            current.DepartmentId      = userList[lstUsers.SelectedIndex].DepartmentId;
            current.Username          = userList[lstUsers.SelectedIndex].Username;
            current.Password          = userList[lstUsers.SelectedIndex].Password;
            current.LastLoginDate     = userList[lstUsers.SelectedIndex].LastLoginDate;
            current.Note = userList[lstUsers.SelectedIndex].Note;

            if (current.UserId != information.UserId)
            {
                string json = "";
                json = JsonConvert.SerializeObject(current);
                json = "=" + json;

                string path = "";
                path = information.URL + "/api/users/delete";

                string response = ApiConnector.SendToApi(path, json);
                if (response == "\"Pass!\"")
                {
                    MessageBox.Show("User successfully removed from system!");
                    // Clean up list box.
                    lstUsers.DataSource    = null;
                    lstUsers.DataSource    = userList;
                    lstUsers.DisplayMember = "Username";
                    lstUsers.ValueMember   = "UserId";
                }
                else
                {
                    MessageBox.Show("Something went wrong, please try again!");
                }
            }
            else
            {
                MessageBox.Show("You cant delete yourself!");
            }
        }
コード例 #2
0
ファイル: EditOrAddUser.cs プロジェクト: antseggs/PamDesktop
        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (btnSave.Text == "Save")
                {
                    // Add new user
                    UserRemoveOrEdit newUser = new UserRemoveOrEdit();
                    newUser.UserId            = -1;
                    newUser.SessionKey        = information.Token;
                    newUser.PermissionLevelId = cmbAccessLevel.SelectedIndex + 1;
                    newUser.FirstName         = txtFirstName.Text;
                    newUser.Surname           = txtSurname.Text;
                    newUser.JobTitle          = txtJobTitle.Text;
                    newUser.DepartmentId      = cmbDepartments.SelectedIndex + 1;
                    newUser.Username          = txtUsername.Text;

                    //SALT AND HASH! // Do this! /// Dont ignore it! //********************************
                    HashAlgorithm algo    = new SHA256Managed();
                    var           hash    = algo.ComputeHash(Encoding.ASCII.GetBytes("quis" + txtPassword.Text + "quam"));
                    string        hexHash = "";
                    for (int i = 0; i < hash.Length; i++)
                    {
                        hexHash = hexHash + hash[i].ToString();
                    }
                    newUser.Password = hexHash;

                    newUser.LastLoginDate = DateTime.Now.ToString("yyy-MM-dd HH:mm:ss.fff");
                    newUser.Note          = txtNotes.Text;

                    // Send off to the API
                    var json = JsonConvert.SerializeObject(newUser);
                    json = "=" + json;

                    var path = information.URL + "/api/users";

                    var response = ApiConnector.SendToApi(path, json);

                    if (response.Contains("Passed!"))
                    {
                        MessageBox.Show("User added!");
                        var temp = response.Substring(1, 2);
                        newUser.UserId = Int32.Parse(temp);
                    }

                    GetAccessLevel sendlvl = new GetAccessLevel();
                    sendlvl.SessionKey = information.Token;
                    sendlvl.Id         = 1;

                    json = JsonConvert.SerializeObject(sendlvl);
                    json = "=" + json;

                    path = "";
                    path = information.URL + "/api/protectedAccount/getAll";

                    response = ApiConnector.SendToApi(path, json);
                    var serverList = JsonConvert.DeserializeObject <List <ServerAccessLevel> >(response);
                    ServerAccessLogedIn accessToSend = new ServerAccessLogedIn();

                    // Add the user permissions bassed on access Level Selected
                    if (cmbAccessLevel.SelectedIndex < 4)
                    {
                        // For all servers
                        foreach (ServerAccessLevel access in serverList)
                        {
                            // Call API for server info
                            GetAccessLevel currServer = new GetAccessLevel();
                            currServer.SessionKey = information.Token;
                            currServer.Id         = access.ServerId;

                            json = JsonConvert.SerializeObject(currServer);
                            json = "=" + json;
                            path = information.URL + "/api/servers/get";

                            // Add to server list
                            response = ApiConnector.SendToApi(path, json);

                            var objec = JsonConvert.DeserializeObject <Server>(response);

                            if (objec.ServerId != 5)
                            {
                                accessToSend.SessionKey        = information.Token;
                                accessToSend.ServerAccessId    = -1;
                                accessToSend.UserId            = newUser.UserId;
                                accessToSend.DepartmentId      = cmbDepartments.SelectedIndex + 1;
                                accessToSend.ServerId          = objec.ServerId;
                                accessToSend.StartTime         = null;
                                accessToSend.FinishTime        = null;
                                accessToSend.StandardAccountId = access.StandardAccountId;
                                accessToSend.Allowed           = true;
                            }

                            json = JsonConvert.SerializeObject(accessToSend);
                            json = "=" + json;
                            path = information.URL + "/api/serverAccessLevel";

                            response = ApiConnector.SendToApi(path, json);
                        }
                    }
                    else if (cmbAccessLevel.SelectedIndex < 5)
                    {
                        // For only linux servers
                        foreach (ServerAccessLevel access in serverList)
                        {
                            // Call API for server info
                            GetAccessLevel currServer = new GetAccessLevel();
                            currServer.SessionKey = information.Token;
                            currServer.Id         = access.ServerId;

                            json = JsonConvert.SerializeObject(currServer);
                            json = "=" + json;
                            path = information.URL + "/api/servers/get";

                            // Add to server list
                            response = ApiConnector.SendToApi(path, json);

                            var objec = JsonConvert.DeserializeObject <Server>(response);
                            if (objec.ServerId != 5 && objec.ServerOsId != 1 && objec.ServerOsId != 2)
                            {
                                //Create the access level
                                accessToSend.UserId            = newUser.UserId;
                                accessToSend.DepartmentId      = current.DepartmentId;
                                accessToSend.ServerId          = objec.ServerId;
                                accessToSend.StartTime         = null;
                                accessToSend.FinishTime        = null;
                                accessToSend.StandardAccountId = access.StandardAccountId;
                                accessToSend.Allowed           = true;

                                json = JsonConvert.SerializeObject(accessToSend);
                                json = "=" + json;
                                path = information.URL + "/api/serverAccessLevel";

                                response = ApiConnector.SendToApi(path, json);
                            }
                        }
                    }
                    else
                    {
                        // Add permissions for scripts only!
                        foreach (ServerAccessLevel access in serverList)
                        {
                            // Call API for server info
                            GetAccessLevel currServer = new GetAccessLevel();
                            currServer.SessionKey = information.Token;
                            currServer.Id         = access.ServerId;

                            json = JsonConvert.SerializeObject(currServer);
                            json = "=" + json;
                            path = information.URL + "/api/servers/get";

                            // Add to server list
                            response = ApiConnector.SendToApi(path, json);

                            var objec = JsonConvert.DeserializeObject <Server>(response);
                            if (objec.ServerId == 5)
                            {
                                //Create the access level
                                accessToSend.UserId            = newUser.UserId;
                                accessToSend.DepartmentId      = current.DepartmentId;
                                accessToSend.ServerId          = objec.ServerId;
                                accessToSend.StartTime         = null;
                                accessToSend.FinishTime        = null;
                                accessToSend.StandardAccountId = access.StandardAccountId;
                                accessToSend.Allowed           = true;

                                json = JsonConvert.SerializeObject(accessToSend);
                                json = "=" + json;
                                path = information.URL + "/api/serverAccessLevel";

                                response = ApiConnector.SendToApi(path, json);
                            }
                        }
                    }
                }
                else
                {
                    // Update User
                    UserRemoveOrEdit newUser = new UserRemoveOrEdit();
                    newUser.UserId            = Int32.Parse(txtUserId.Text);
                    newUser.SessionKey        = information.Token;
                    newUser.PermissionLevelId = cmbAccessLevel.SelectedIndex + 1;
                    newUser.FirstName         = txtFirstName.Text;
                    newUser.Surname           = txtSurname.Text;
                    newUser.JobTitle          = txtJobTitle.Text;
                    newUser.DepartmentId      = cmbDepartments.SelectedIndex + 1;
                    newUser.Password          = current.Password;
                    newUser.Username          = current.Username;
                    newUser.LastLoginDate     = DateTime.Now.ToString("yyy-MM-dd HH:mm:ss.fff");
                    newUser.Note = txtNotes.Text;

                    // Send off to the API
                    var json = JsonConvert.SerializeObject(newUser);
                    json = "=" + json;

                    var path = information.URL + "/api/users";

                    var response = ApiConnector.SendToApi(path, json);

                    if (response == "\"Passed!\"")
                    {
                        MessageBox.Show("User Updated!");
                    }
                    if (response == "\"Username Clash!\"")
                    {
                        MessageBox.Show("Duplicated username!, Please try again");
                    }
                }
                this.Close();
            }
            catch (Exception ex)
            {
            }
        }