Rename AuthorizationError to ValidationError, rename suberrors
All checks were successful
All checks were successful
This commit is contained in:
@@ -4,15 +4,15 @@ use serde::{Deserialize, Serialize};
|
||||
use serde_json::json;
|
||||
use thiserror::Error;
|
||||
|
||||
use crate::core::{protocol::request_validation::AuthorizationError, types::DbOrUser};
|
||||
use crate::core::{protocol::request_validation::ValidationError, types::DbOrUser};
|
||||
|
||||
pub type CheckAuthorizationRequest = Vec<DbOrUser>;
|
||||
|
||||
pub type CheckAuthorizationResponse = BTreeMap<DbOrUser, Result<(), CheckAuthorizationError>>;
|
||||
|
||||
#[derive(Error, Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
#[error("Authorization error: {0}")]
|
||||
pub struct CheckAuthorizationError(#[from] pub AuthorizationError);
|
||||
#[error("Validation error: {0}")]
|
||||
pub struct CheckAuthorizationError(#[from] pub ValidationError);
|
||||
|
||||
pub fn print_check_authorization_output_status(output: &CheckAuthorizationResponse) {
|
||||
for (db_or_user, result) in output {
|
||||
|
||||
@@ -5,7 +5,7 @@ use serde_json::json;
|
||||
use thiserror::Error;
|
||||
|
||||
use crate::core::{
|
||||
protocol::request_validation::AuthorizationError,
|
||||
protocol::request_validation::ValidationError,
|
||||
types::{DbOrUser, MySQLDatabase},
|
||||
};
|
||||
|
||||
@@ -15,8 +15,8 @@ pub type CreateDatabasesResponse = BTreeMap<MySQLDatabase, Result<(), CreateData
|
||||
|
||||
#[derive(Error, Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
pub enum CreateDatabaseError {
|
||||
#[error("Authorization error: {0}")]
|
||||
AuthorizationError(#[from] AuthorizationError),
|
||||
#[error("Validation error: {0}")]
|
||||
ValidationError(#[from] ValidationError),
|
||||
|
||||
#[error("Database already exists")]
|
||||
DatabaseAlreadyExists,
|
||||
@@ -65,7 +65,7 @@ pub fn print_create_databases_output_status_json(output: &CreateDatabasesRespons
|
||||
impl CreateDatabaseError {
|
||||
pub fn to_error_message(&self, database_name: &MySQLDatabase) -> String {
|
||||
match self {
|
||||
CreateDatabaseError::AuthorizationError(err) => {
|
||||
CreateDatabaseError::ValidationError(err) => {
|
||||
err.to_error_message(DbOrUser::Database(database_name.clone()))
|
||||
}
|
||||
CreateDatabaseError::DatabaseAlreadyExists => {
|
||||
@@ -79,7 +79,7 @@ impl CreateDatabaseError {
|
||||
|
||||
pub fn error_type(&self) -> String {
|
||||
match self {
|
||||
CreateDatabaseError::AuthorizationError(err) => err.error_type(),
|
||||
CreateDatabaseError::ValidationError(err) => err.error_type(),
|
||||
CreateDatabaseError::DatabaseAlreadyExists => "database-already-exists".to_string(),
|
||||
CreateDatabaseError::MySqlError(_) => "mysql-error".to_string(),
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ use serde_json::json;
|
||||
use thiserror::Error;
|
||||
|
||||
use crate::core::{
|
||||
protocol::request_validation::AuthorizationError,
|
||||
protocol::request_validation::ValidationError,
|
||||
types::{DbOrUser, MySQLUser},
|
||||
};
|
||||
|
||||
@@ -15,8 +15,8 @@ pub type CreateUsersResponse = BTreeMap<MySQLUser, Result<(), CreateUserError>>;
|
||||
|
||||
#[derive(Error, Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
pub enum CreateUserError {
|
||||
#[error("Authorization error: {0}")]
|
||||
AuthorizationError(#[from] AuthorizationError),
|
||||
#[error("Validation error: {0}")]
|
||||
ValidationError(#[from] ValidationError),
|
||||
|
||||
#[error("User already exists")]
|
||||
UserAlreadyExists,
|
||||
@@ -65,7 +65,7 @@ pub fn print_create_users_output_status_json(output: &CreateUsersResponse) {
|
||||
impl CreateUserError {
|
||||
pub fn to_error_message(&self, username: &MySQLUser) -> String {
|
||||
match self {
|
||||
CreateUserError::AuthorizationError(err) => {
|
||||
CreateUserError::ValidationError(err) => {
|
||||
err.to_error_message(DbOrUser::User(username.clone()))
|
||||
}
|
||||
CreateUserError::UserAlreadyExists => {
|
||||
@@ -79,7 +79,7 @@ impl CreateUserError {
|
||||
|
||||
pub fn error_type(&self) -> String {
|
||||
match self {
|
||||
CreateUserError::AuthorizationError(err) => err.error_type(),
|
||||
CreateUserError::ValidationError(err) => err.error_type(),
|
||||
CreateUserError::UserAlreadyExists => "user-already-exists".to_string(),
|
||||
CreateUserError::MySqlError(_) => "mysql-error".to_string(),
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ use serde_json::json;
|
||||
use thiserror::Error;
|
||||
|
||||
use crate::core::{
|
||||
protocol::request_validation::AuthorizationError,
|
||||
protocol::request_validation::ValidationError,
|
||||
types::{DbOrUser, MySQLDatabase},
|
||||
};
|
||||
|
||||
@@ -15,8 +15,8 @@ pub type DropDatabasesResponse = BTreeMap<MySQLDatabase, Result<(), DropDatabase
|
||||
|
||||
#[derive(Error, Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
pub enum DropDatabaseError {
|
||||
#[error("Authorization error: {0}")]
|
||||
AuthorizationError(#[from] AuthorizationError),
|
||||
#[error("Validation error: {0}")]
|
||||
ValidationError(#[from] ValidationError),
|
||||
|
||||
#[error("Database does not exist")]
|
||||
DatabaseDoesNotExist,
|
||||
@@ -68,7 +68,7 @@ pub fn print_drop_databases_output_status_json(output: &DropDatabasesResponse) {
|
||||
impl DropDatabaseError {
|
||||
pub fn to_error_message(&self, database_name: &MySQLDatabase) -> String {
|
||||
match self {
|
||||
DropDatabaseError::AuthorizationError(err) => {
|
||||
DropDatabaseError::ValidationError(err) => {
|
||||
err.to_error_message(DbOrUser::Database(database_name.clone()))
|
||||
}
|
||||
DropDatabaseError::DatabaseDoesNotExist => {
|
||||
@@ -82,7 +82,7 @@ impl DropDatabaseError {
|
||||
|
||||
pub fn error_type(&self) -> String {
|
||||
match self {
|
||||
DropDatabaseError::AuthorizationError(err) => err.error_type(),
|
||||
DropDatabaseError::ValidationError(err) => err.error_type(),
|
||||
DropDatabaseError::DatabaseDoesNotExist => "database-does-not-exist".to_string(),
|
||||
DropDatabaseError::MySqlError(_) => "mysql-error".to_string(),
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ use serde_json::json;
|
||||
use thiserror::Error;
|
||||
|
||||
use crate::core::{
|
||||
protocol::request_validation::AuthorizationError,
|
||||
protocol::request_validation::ValidationError,
|
||||
types::{DbOrUser, MySQLUser},
|
||||
};
|
||||
|
||||
@@ -15,8 +15,8 @@ pub type DropUsersResponse = BTreeMap<MySQLUser, Result<(), DropUserError>>;
|
||||
|
||||
#[derive(Error, Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
pub enum DropUserError {
|
||||
#[error("Authorization error: {0}")]
|
||||
AuthorizationError(#[from] AuthorizationError),
|
||||
#[error("Validation error: {0}")]
|
||||
ValidationError(#[from] ValidationError),
|
||||
|
||||
#[error("User does not exist")]
|
||||
UserDoesNotExist,
|
||||
@@ -65,7 +65,7 @@ pub fn print_drop_users_output_status_json(output: &DropUsersResponse) {
|
||||
impl DropUserError {
|
||||
pub fn to_error_message(&self, username: &MySQLUser) -> String {
|
||||
match self {
|
||||
DropUserError::AuthorizationError(err) => {
|
||||
DropUserError::ValidationError(err) => {
|
||||
err.to_error_message(DbOrUser::User(username.clone()))
|
||||
}
|
||||
DropUserError::UserDoesNotExist => {
|
||||
@@ -79,7 +79,7 @@ impl DropUserError {
|
||||
|
||||
pub fn error_type(&self) -> String {
|
||||
match self {
|
||||
DropUserError::AuthorizationError(err) => err.error_type(),
|
||||
DropUserError::ValidationError(err) => err.error_type(),
|
||||
DropUserError::UserDoesNotExist => "user-does-not-exist".to_string(),
|
||||
DropUserError::MySqlError(_) => "mysql-error".to_string(),
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ use thiserror::Error;
|
||||
|
||||
use crate::{
|
||||
core::{
|
||||
protocol::request_validation::AuthorizationError,
|
||||
protocol::request_validation::ValidationError,
|
||||
types::{DbOrUser, MySQLDatabase},
|
||||
},
|
||||
server::sql::database_operations::DatabaseRow,
|
||||
@@ -20,8 +20,8 @@ pub type ListDatabasesResponse = BTreeMap<MySQLDatabase, Result<DatabaseRow, Lis
|
||||
|
||||
#[derive(Error, Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
pub enum ListDatabasesError {
|
||||
#[error("Authorization error: {0}")]
|
||||
AuthorizationError(#[from] AuthorizationError),
|
||||
#[error("Validation error: {0}")]
|
||||
ValidationError(#[from] ValidationError),
|
||||
|
||||
#[error("Database does not exist")]
|
||||
DatabaseDoesNotExist,
|
||||
@@ -104,7 +104,7 @@ pub fn print_list_databases_output_status_json(output: &ListDatabasesResponse) {
|
||||
impl ListDatabasesError {
|
||||
pub fn to_error_message(&self, database_name: &MySQLDatabase) -> String {
|
||||
match self {
|
||||
ListDatabasesError::AuthorizationError(err) => {
|
||||
ListDatabasesError::ValidationError(err) => {
|
||||
err.to_error_message(DbOrUser::Database(database_name.clone()))
|
||||
}
|
||||
ListDatabasesError::DatabaseDoesNotExist => {
|
||||
@@ -118,7 +118,7 @@ impl ListDatabasesError {
|
||||
|
||||
pub fn error_type(&self) -> String {
|
||||
match self {
|
||||
ListDatabasesError::AuthorizationError(err) => err.error_type(),
|
||||
ListDatabasesError::ValidationError(err) => err.error_type(),
|
||||
ListDatabasesError::DatabaseDoesNotExist => "database-does-not-exist".to_string(),
|
||||
ListDatabasesError::MySqlError(_) => "mysql-error".to_string(),
|
||||
}
|
||||
|
||||
@@ -16,7 +16,7 @@ use crate::core::{
|
||||
DATABASE_PRIVILEGE_FIELDS, DatabasePrivilegeRow, db_priv_field_human_readable_name,
|
||||
db_priv_field_single_character_name,
|
||||
},
|
||||
protocol::request_validation::AuthorizationError,
|
||||
protocol::request_validation::ValidationError,
|
||||
types::{DbOrUser, MySQLDatabase},
|
||||
};
|
||||
|
||||
@@ -118,8 +118,8 @@ pub fn print_list_privileges_output_status_json(output: &ListPrivilegesResponse)
|
||||
|
||||
#[derive(Error, Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
pub enum GetDatabasesPrivilegeDataError {
|
||||
#[error("Authorization error: {0}")]
|
||||
AuthorizationError(#[from] AuthorizationError),
|
||||
#[error("Validation error: {0}")]
|
||||
ValidationError(#[from] ValidationError),
|
||||
|
||||
#[error("Database does not exist")]
|
||||
DatabaseDoesNotExist,
|
||||
@@ -131,7 +131,7 @@ pub enum GetDatabasesPrivilegeDataError {
|
||||
impl GetDatabasesPrivilegeDataError {
|
||||
pub fn to_error_message(&self, database_name: &MySQLDatabase) -> String {
|
||||
match self {
|
||||
GetDatabasesPrivilegeDataError::AuthorizationError(err) => {
|
||||
GetDatabasesPrivilegeDataError::ValidationError(err) => {
|
||||
err.to_error_message(DbOrUser::Database(database_name.clone()))
|
||||
}
|
||||
GetDatabasesPrivilegeDataError::DatabaseDoesNotExist => {
|
||||
@@ -145,7 +145,7 @@ impl GetDatabasesPrivilegeDataError {
|
||||
|
||||
pub fn error_type(&self) -> String {
|
||||
match self {
|
||||
GetDatabasesPrivilegeDataError::AuthorizationError(err) => err.error_type(),
|
||||
GetDatabasesPrivilegeDataError::ValidationError(err) => err.error_type(),
|
||||
GetDatabasesPrivilegeDataError::DatabaseDoesNotExist => {
|
||||
"database-does-not-exist".to_string()
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@ use thiserror::Error;
|
||||
|
||||
use crate::{
|
||||
core::{
|
||||
protocol::request_validation::AuthorizationError,
|
||||
protocol::request_validation::ValidationError,
|
||||
types::{DbOrUser, MySQLUser},
|
||||
},
|
||||
server::sql::user_operations::DatabaseUser,
|
||||
@@ -19,8 +19,8 @@ pub type ListUsersResponse = BTreeMap<MySQLUser, Result<DatabaseUser, ListUsersE
|
||||
|
||||
#[derive(Error, Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
pub enum ListUsersError {
|
||||
#[error("Authorization error: {0}")]
|
||||
AuthorizationError(#[from] AuthorizationError),
|
||||
#[error("Validation error: {0}")]
|
||||
ValidationError(#[from] ValidationError),
|
||||
|
||||
#[error("User does not exist")]
|
||||
UserDoesNotExist,
|
||||
@@ -99,7 +99,7 @@ pub fn print_list_users_output_status_json(output: &ListUsersResponse) {
|
||||
impl ListUsersError {
|
||||
pub fn to_error_message(&self, username: &MySQLUser) -> String {
|
||||
match self {
|
||||
ListUsersError::AuthorizationError(err) => {
|
||||
ListUsersError::ValidationError(err) => {
|
||||
err.to_error_message(DbOrUser::User(username.clone()))
|
||||
}
|
||||
ListUsersError::UserDoesNotExist => {
|
||||
@@ -113,7 +113,7 @@ impl ListUsersError {
|
||||
|
||||
pub fn error_type(&self) -> String {
|
||||
match self {
|
||||
ListUsersError::AuthorizationError(err) => err.error_type(),
|
||||
ListUsersError::ValidationError(err) => err.error_type(),
|
||||
ListUsersError::UserDoesNotExist => "user-does-not-exist".to_string(),
|
||||
ListUsersError::MySqlError(_) => "mysql-error".to_string(),
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ use serde_json::json;
|
||||
use thiserror::Error;
|
||||
|
||||
use crate::core::{
|
||||
protocol::request_validation::AuthorizationError,
|
||||
protocol::request_validation::ValidationError,
|
||||
types::{DbOrUser, MySQLUser},
|
||||
};
|
||||
|
||||
@@ -15,8 +15,8 @@ pub type LockUsersResponse = BTreeMap<MySQLUser, Result<(), LockUserError>>;
|
||||
|
||||
#[derive(Error, Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
pub enum LockUserError {
|
||||
#[error("Authorization error: {0}")]
|
||||
AuthorizationError(#[from] AuthorizationError),
|
||||
#[error("Validation error: {0}")]
|
||||
ValidationError(#[from] ValidationError),
|
||||
|
||||
#[error("User does not exist")]
|
||||
UserDoesNotExist,
|
||||
@@ -68,7 +68,7 @@ pub fn print_lock_users_output_status_json(output: &LockUsersResponse) {
|
||||
impl LockUserError {
|
||||
pub fn to_error_message(&self, username: &MySQLUser) -> String {
|
||||
match self {
|
||||
LockUserError::AuthorizationError(err) => {
|
||||
LockUserError::ValidationError(err) => {
|
||||
err.to_error_message(DbOrUser::User(username.clone()))
|
||||
}
|
||||
LockUserError::UserDoesNotExist => {
|
||||
@@ -85,7 +85,7 @@ impl LockUserError {
|
||||
|
||||
pub fn error_type(&self) -> String {
|
||||
match self {
|
||||
LockUserError::AuthorizationError(err) => err.error_type(),
|
||||
LockUserError::ValidationError(err) => err.error_type(),
|
||||
LockUserError::UserDoesNotExist => "user-does-not-exist".to_string(),
|
||||
LockUserError::UserIsAlreadyLocked => "user-is-already-locked".to_string(),
|
||||
LockUserError::MySqlError(_) => "mysql-error".to_string(),
|
||||
|
||||
@@ -5,7 +5,7 @@ use thiserror::Error;
|
||||
|
||||
use crate::core::{
|
||||
database_privileges::{DatabasePrivilegeRow, DatabasePrivilegeRowDiff, DatabasePrivilegesDiff},
|
||||
protocol::request_validation::AuthorizationError,
|
||||
protocol::request_validation::ValidationError,
|
||||
types::{DbOrUser, MySQLDatabase, MySQLUser},
|
||||
};
|
||||
|
||||
@@ -16,11 +16,11 @@ pub type ModifyPrivilegesResponse =
|
||||
|
||||
#[derive(Error, Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
pub enum ModifyDatabasePrivilegesError {
|
||||
#[error("Database authorization error: {0}")]
|
||||
DatabaseAuthorizationError(AuthorizationError),
|
||||
#[error("Database validation error: {0}")]
|
||||
DatabaseValidationError(ValidationError),
|
||||
|
||||
#[error("User authorization error: {0}")]
|
||||
UserAuthorizationError(AuthorizationError),
|
||||
#[error("User validation error: {0}")]
|
||||
UserValidationError(ValidationError),
|
||||
|
||||
#[error("Database does not exist")]
|
||||
DatabaseDoesNotExist,
|
||||
@@ -69,10 +69,10 @@ pub fn print_modify_database_privileges_output_status(output: &ModifyPrivilegesR
|
||||
impl ModifyDatabasePrivilegesError {
|
||||
pub fn to_error_message(&self, database_name: &MySQLDatabase, username: &MySQLUser) -> String {
|
||||
match self {
|
||||
ModifyDatabasePrivilegesError::DatabaseAuthorizationError(err) => {
|
||||
ModifyDatabasePrivilegesError::DatabaseValidationError(err) => {
|
||||
err.to_error_message(DbOrUser::Database(database_name.clone()))
|
||||
}
|
||||
ModifyDatabasePrivilegesError::UserAuthorizationError(err) => {
|
||||
ModifyDatabasePrivilegesError::UserValidationError(err) => {
|
||||
err.to_error_message(DbOrUser::User(username.clone()))
|
||||
}
|
||||
ModifyDatabasePrivilegesError::DatabaseDoesNotExist => {
|
||||
@@ -97,8 +97,8 @@ impl ModifyDatabasePrivilegesError {
|
||||
pub fn error_type(&self) -> String {
|
||||
match self {
|
||||
// TODO: should these be subtyped?
|
||||
ModifyDatabasePrivilegesError::DatabaseAuthorizationError(err) => err.error_type(),
|
||||
ModifyDatabasePrivilegesError::UserAuthorizationError(err) => err.error_type(),
|
||||
ModifyDatabasePrivilegesError::DatabaseValidationError(err) => err.error_type(),
|
||||
ModifyDatabasePrivilegesError::UserValidationError(err) => err.error_type(),
|
||||
ModifyDatabasePrivilegesError::DatabaseDoesNotExist => {
|
||||
"database-does-not-exist".to_string()
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ use serde::{Deserialize, Serialize};
|
||||
use thiserror::Error;
|
||||
|
||||
use crate::core::{
|
||||
protocol::request_validation::AuthorizationError,
|
||||
protocol::request_validation::ValidationError,
|
||||
types::{DbOrUser, MySQLUser},
|
||||
};
|
||||
|
||||
@@ -12,8 +12,8 @@ pub type SetUserPasswordResponse = Result<(), SetPasswordError>;
|
||||
|
||||
#[derive(Error, Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
pub enum SetPasswordError {
|
||||
#[error("Authorization error: {0}")]
|
||||
AuthorizationError(#[from] AuthorizationError),
|
||||
#[error("Validation error: {0}")]
|
||||
ValidationError(#[from] ValidationError),
|
||||
|
||||
#[error("User does not exist")]
|
||||
UserDoesNotExist,
|
||||
@@ -37,7 +37,7 @@ pub fn print_set_password_output_status(output: &SetUserPasswordResponse, userna
|
||||
impl SetPasswordError {
|
||||
pub fn to_error_message(&self, username: &MySQLUser) -> String {
|
||||
match self {
|
||||
SetPasswordError::AuthorizationError(err) => {
|
||||
SetPasswordError::ValidationError(err) => {
|
||||
err.to_error_message(DbOrUser::User(username.clone()))
|
||||
}
|
||||
SetPasswordError::UserDoesNotExist => {
|
||||
@@ -52,7 +52,7 @@ impl SetPasswordError {
|
||||
#[allow(dead_code)]
|
||||
pub fn error_type(&self) -> String {
|
||||
match self {
|
||||
SetPasswordError::AuthorizationError(err) => err.error_type(),
|
||||
SetPasswordError::ValidationError(err) => err.error_type(),
|
||||
SetPasswordError::UserDoesNotExist => "user-does-not-exist".to_string(),
|
||||
SetPasswordError::MySqlError(_) => "mysql-error".to_string(),
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ use serde_json::json;
|
||||
use thiserror::Error;
|
||||
|
||||
use crate::core::{
|
||||
protocol::request_validation::AuthorizationError,
|
||||
protocol::request_validation::ValidationError,
|
||||
types::{DbOrUser, MySQLUser},
|
||||
};
|
||||
|
||||
@@ -15,8 +15,8 @@ pub type UnlockUsersResponse = BTreeMap<MySQLUser, Result<(), UnlockUserError>>;
|
||||
|
||||
#[derive(Error, Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
pub enum UnlockUserError {
|
||||
#[error("Authorization error: {0}")]
|
||||
AuthorizationError(#[from] AuthorizationError),
|
||||
#[error("Validation error: {0}")]
|
||||
ValidationError(#[from] ValidationError),
|
||||
|
||||
#[error("User does not exist")]
|
||||
UserDoesNotExist,
|
||||
@@ -68,7 +68,7 @@ pub fn print_unlock_users_output_status_json(output: &UnlockUsersResponse) {
|
||||
impl UnlockUserError {
|
||||
pub fn to_error_message(&self, username: &MySQLUser) -> String {
|
||||
match self {
|
||||
UnlockUserError::AuthorizationError(err) => {
|
||||
UnlockUserError::ValidationError(err) => {
|
||||
err.to_error_message(DbOrUser::User(username.clone()))
|
||||
}
|
||||
UnlockUserError::UserDoesNotExist => {
|
||||
@@ -85,7 +85,7 @@ impl UnlockUserError {
|
||||
|
||||
pub fn error_type(&self) -> String {
|
||||
match self {
|
||||
UnlockUserError::AuthorizationError(err) => err.error_type(),
|
||||
UnlockUserError::ValidationError(err) => err.error_type(),
|
||||
UnlockUserError::UserDoesNotExist => "user-does-not-exist".to_string(),
|
||||
UnlockUserError::UserIsAlreadyUnlocked => "user-is-already-unlocked".to_string(),
|
||||
UnlockUserError::MySqlError(_) => "mysql-error".to_string(),
|
||||
|
||||
@@ -53,15 +53,16 @@ impl NameValidationError {
|
||||
}
|
||||
|
||||
#[derive(Error, Debug, PartialEq, Eq, Clone, Copy, Serialize, Deserialize)]
|
||||
pub enum OwnerValidationError {
|
||||
pub enum AuthorizationError {
|
||||
#[error("No matching owner prefix found")]
|
||||
NoMatch,
|
||||
|
||||
// TODO: I don't think this should ever happen?
|
||||
#[error("Name cannot be empty")]
|
||||
StringEmpty,
|
||||
}
|
||||
|
||||
impl OwnerValidationError {
|
||||
impl AuthorizationError {
|
||||
pub fn to_error_message(self, db_or_user: DbOrUser) -> String {
|
||||
let user = UnixUser::from_enviroment();
|
||||
|
||||
@@ -76,7 +77,7 @@ impl OwnerValidationError {
|
||||
groups.sort();
|
||||
|
||||
match self {
|
||||
OwnerValidationError::NoMatch => format!(
|
||||
AuthorizationError::NoMatch => format!(
|
||||
indoc! {r#"
|
||||
Invalid {} name prefix: '{}' does not match your username or any of your groups.
|
||||
Are you sure you are allowed to create {} names with this prefix?
|
||||
@@ -98,7 +99,7 @@ impl OwnerValidationError {
|
||||
.join("\n"),
|
||||
)
|
||||
.to_owned(),
|
||||
OwnerValidationError::StringEmpty => format!(
|
||||
AuthorizationError::StringEmpty => format!(
|
||||
"'{}' is not a valid {} name.",
|
||||
db_or_user.name(),
|
||||
db_or_user.lowercased_noun()
|
||||
@@ -109,27 +110,27 @@ impl OwnerValidationError {
|
||||
|
||||
pub fn error_type(&self) -> &'static str {
|
||||
match self {
|
||||
OwnerValidationError::NoMatch => "no-match",
|
||||
OwnerValidationError::StringEmpty => "string-empty",
|
||||
AuthorizationError::NoMatch => "no-match",
|
||||
AuthorizationError::StringEmpty => "string-empty",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Error, Debug, PartialEq, Eq, Clone, Serialize, Deserialize)]
|
||||
pub enum AuthorizationError {
|
||||
#[error("Sanitization error: {0}")]
|
||||
SanitizationError(NameValidationError),
|
||||
pub enum ValidationError {
|
||||
#[error("Name validation error: {0}")]
|
||||
NameValidationError(NameValidationError),
|
||||
|
||||
#[error("Ownership error: {0}")]
|
||||
OwnershipError(OwnerValidationError),
|
||||
#[error("Authorization error: {0}")]
|
||||
AuthorizationError(AuthorizationError),
|
||||
// AuthorizationHandlerError(String),
|
||||
}
|
||||
|
||||
impl AuthorizationError {
|
||||
impl ValidationError {
|
||||
pub fn to_error_message(&self, db_or_user: DbOrUser) -> String {
|
||||
match self {
|
||||
AuthorizationError::SanitizationError(err) => err.to_error_message(db_or_user),
|
||||
AuthorizationError::OwnershipError(err) => err.to_error_message(db_or_user),
|
||||
ValidationError::NameValidationError(err) => err.to_error_message(db_or_user),
|
||||
ValidationError::AuthorizationError(err) => err.to_error_message(db_or_user),
|
||||
// AuthorizationError::AuthorizationHandlerError(msg) => {
|
||||
// format!(
|
||||
// "Authorization handler error for '{}': {}",
|
||||
@@ -142,12 +143,11 @@ impl AuthorizationError {
|
||||
|
||||
pub fn error_type(&self) -> String {
|
||||
match self {
|
||||
AuthorizationError::SanitizationError(err) => {
|
||||
format!("sanitization-error/{}", err.error_type())
|
||||
ValidationError::NameValidationError(err) => {
|
||||
format!("name-validation-error/{}", err.error_type())
|
||||
}
|
||||
// TODO: maybe rename this to authorization error?
|
||||
AuthorizationError::OwnershipError(err) => {
|
||||
format!("ownership-error/{}", err.error_type())
|
||||
ValidationError::AuthorizationError(err) => {
|
||||
format!("authorization-error/{}", err.error_type())
|
||||
} // AuthorizationError::AuthorizationHandlerError(_) => {
|
||||
// "authorization-handler-error".to_string()
|
||||
// }
|
||||
@@ -172,27 +172,27 @@ pub fn validate_name(name: &str) -> Result<(), NameValidationError> {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn validate_ownership_by_unix_user(
|
||||
pub fn validate_authorization_by_unix_user(
|
||||
name: &str,
|
||||
user: &UnixUser,
|
||||
) -> Result<(), OwnerValidationError> {
|
||||
) -> Result<(), AuthorizationError> {
|
||||
let prefixes = std::iter::once(user.username.to_owned())
|
||||
.chain(user.groups.iter().cloned())
|
||||
.collect::<Vec<String>>();
|
||||
|
||||
validate_ownership_by_prefixes(name, &prefixes)
|
||||
validate_authorization_by_prefixes(name, &prefixes)
|
||||
}
|
||||
|
||||
/// Core logic for validating the ownership of a database name.
|
||||
/// This function checks if the given name matches any of the given prefixes.
|
||||
/// These prefixes will in most cases be the user's unix username and any
|
||||
/// unix groups the user is a member of.
|
||||
pub fn validate_ownership_by_prefixes(
|
||||
pub fn validate_authorization_by_prefixes(
|
||||
name: &str,
|
||||
prefixes: &[String],
|
||||
) -> Result<(), OwnerValidationError> {
|
||||
) -> Result<(), AuthorizationError> {
|
||||
if name.is_empty() {
|
||||
return Err(OwnerValidationError::StringEmpty);
|
||||
return Err(AuthorizationError::StringEmpty);
|
||||
}
|
||||
|
||||
if prefixes
|
||||
@@ -201,7 +201,7 @@ pub fn validate_ownership_by_prefixes(
|
||||
.collect::<Vec<_>>()
|
||||
.is_empty()
|
||||
{
|
||||
return Err(OwnerValidationError::NoMatch);
|
||||
return Err(AuthorizationError::NoMatch);
|
||||
};
|
||||
|
||||
Ok(())
|
||||
@@ -210,11 +210,11 @@ pub fn validate_ownership_by_prefixes(
|
||||
pub fn validate_db_or_user_request(
|
||||
db_or_user: &DbOrUser,
|
||||
unix_user: &UnixUser,
|
||||
) -> Result<(), AuthorizationError> {
|
||||
validate_name(db_or_user.name()).map_err(AuthorizationError::SanitizationError)?;
|
||||
) -> Result<(), ValidationError> {
|
||||
validate_name(db_or_user.name()).map_err(ValidationError::NameValidationError)?;
|
||||
|
||||
validate_ownership_by_unix_user(db_or_user.name(), unix_user)
|
||||
.map_err(AuthorizationError::OwnershipError)?;
|
||||
validate_authorization_by_unix_user(db_or_user.name(), unix_user)
|
||||
.map_err(ValidationError::AuthorizationError)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -246,34 +246,34 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_validate_owner_by_prefixes() {
|
||||
fn test_validate_authorization_by_prefixes() {
|
||||
let prefixes = vec!["user".to_string(), "group".to_string()];
|
||||
|
||||
assert_eq!(
|
||||
validate_ownership_by_prefixes("", &prefixes),
|
||||
Err(OwnerValidationError::StringEmpty)
|
||||
validate_authorization_by_prefixes("", &prefixes),
|
||||
Err(AuthorizationError::StringEmpty)
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
validate_ownership_by_prefixes("user_testdb", &prefixes),
|
||||
validate_authorization_by_prefixes("user_testdb", &prefixes),
|
||||
Ok(())
|
||||
);
|
||||
assert_eq!(
|
||||
validate_ownership_by_prefixes("group_testdb", &prefixes),
|
||||
validate_authorization_by_prefixes("group_testdb", &prefixes),
|
||||
Ok(())
|
||||
);
|
||||
assert_eq!(
|
||||
validate_ownership_by_prefixes("group_test_db", &prefixes),
|
||||
validate_authorization_by_prefixes("group_test_db", &prefixes),
|
||||
Ok(())
|
||||
);
|
||||
assert_eq!(
|
||||
validate_ownership_by_prefixes("group_test-db", &prefixes),
|
||||
validate_authorization_by_prefixes("group_test-db", &prefixes),
|
||||
Ok(())
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
validate_ownership_by_prefixes("nonexistent_testdb", &prefixes),
|
||||
Err(OwnerValidationError::NoMatch)
|
||||
validate_authorization_by_prefixes("nonexistent_testdb", &prefixes),
|
||||
Err(AuthorizationError::NoMatch)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user