You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
56 lines
1.7 KiB
56 lines
1.7 KiB
use std::borrow::Cow;
|
|
use std::error::Error;
|
|
use std::fmt;
|
|
|
|
use command_runner::CommandRunner;
|
|
use resources::Resource;
|
|
use symbols::{Action, OwnedSymbolAction, Symbol, SymbolAction, SymbolRunner};
|
|
|
|
pub struct MariaDBUser<'a, C: 'a + CommandRunner> {
|
|
user_name: Cow<'a, str>,
|
|
command_runner: &'a C
|
|
}
|
|
|
|
impl<'a, C: CommandRunner> MariaDBUser<'a, C> {
|
|
pub fn new(user_name: Cow<'a, str>, command_runner: &'a C) -> Self {
|
|
MariaDBUser { user_name, command_runner }
|
|
}
|
|
|
|
fn run_sql(&self, sql: &str) -> Result<String, Box<dyn Error>> {
|
|
let b = try!(self.command_runner.get_output("mariadb", &["--skip-column-names", "-B", "-e", sql]));
|
|
Ok(try!(String::from_utf8(b)))
|
|
}
|
|
}
|
|
|
|
impl<'a, C: CommandRunner> fmt::Display for MariaDBUser<'a, C> {
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
write!(f, "MariaDB User {}", self.user_name)
|
|
}
|
|
}
|
|
|
|
impl<'a, C: CommandRunner> Symbol for MariaDBUser<'a, C> {
|
|
fn target_reached(&self) -> Result<bool, Box<dyn Error>> {
|
|
Ok(try!(self.run_sql(&format!("SELECT User FROM mysql.user WHERE User = '{}' AND plugin = 'unix_socket'", self.user_name))).trim_end() == self.user_name)
|
|
}
|
|
|
|
fn execute(&self) -> Result<(), Box<dyn Error>> {
|
|
try!(self.run_sql(&format!("GRANT ALL ON {0}.* TO {0} IDENTIFIED VIA unix_socket", self.user_name)));
|
|
Ok(())
|
|
}
|
|
|
|
fn get_prerequisites(&self) -> Vec<Resource> {
|
|
vec![ Resource::new("user", self.user_name.to_string()) ]
|
|
}
|
|
|
|
fn as_action<'b>(&'b self, runner: &'b dyn SymbolRunner) -> Box<dyn Action + 'b> {
|
|
Box::new(SymbolAction::new(runner, self))
|
|
}
|
|
|
|
fn into_action<'b>(self: Box<Self>, runner: &'b dyn SymbolRunner) -> Box<dyn Action + 'b> where Self: 'b {
|
|
Box::new(OwnedSymbolAction::new(runner, *self))
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod test {
|
|
}
|