fn create_or_update_privileges(
    conn: &mut DbConnection,
    config: &Config,
    dryrun: bool,
) -> Result<()> {
    let mut summary = vec![vec![
        "User".to_string(),
        "Role Name".to_string(),
        "Detail".to_string(),
        "Status".to_string(),
    ]];
    summary.push(vec![
        "---".to_string(),
        "---".to_string(),
        "---".to_string(),
        "---".to_string(),
    ]);

    // Loop through users in config
    // Get the user Role object by the user.roles[*].name
    // Apply the Role sql privileges to the cluster
    for user in &config.users {
        // Compare privileges on config and db
        // If privileges on config are not in db, add them
        // If privileges on db are not in config, remove them
        for role_name in user.roles.iter() {
            let role = config.roles.iter().find(|&r| r.find(role_name)).unwrap();

            // TODO: revoke if privileges on db are not in configuration

            let sql = role.to_sql(&user.name);

            let mut status = if dryrun {
                "dry-run".to_string()
            } else {
                "updated".to_string()
            };

            if !dryrun {
                let nrows = conn.execute(&sql, &[]).unwrap_or_else(|e| {
                    error!("{}: {}", Red.paint("Error"), sql);
                    error!("  -> {}: {}", Red.paint("Error details"), e);
                    status = "error".to_string();

                    -1
                });

                if nrows > -1 {
                    info!(
                        "{}: {} {}",
                        Green.paint("Success"),
                        Purple.paint(sql),
                        format!("(updated {} row(s))", nrows)
                    );
                }
            } else {
                info!("{}: {}", Purple.paint("Dry-run"), sql);
            }

            let detail = match role {
                Role::Database(role) => format!("database{:?}", role.databases.clone()),
                Role::Schema(role) => format!("schema{:?}", role.schemas.clone()),
                Role::Table(role) => format!("table{:?}", role.tables.clone()),
            };

            // Update summary
            summary.push(vec![
                user.name.clone(),
                role_name.clone(),
                detail.to_string(),
                status.to_string(),
            ]);
        }
    }

    // Show summary
    print_summary(summary);

    Ok(())
}
/// Render role configuration to SQL and sync with database.
/// If the privileges are not in the database, they will be granted to user.
/// If the privileges are in the database, they will be updated.
/// If the privileges are not in the configuration, they will be revoked from user.
Render role configuration to SQL and sync with database.
If the privileges are not in the database, they will be granted to user.
If the privileges are in the database, they will be updated.
If the privileges are not in the configuration, they will be revoked from user.
Render role configuration to SQL and sync with database . If the privileges are not in the database they will be granted to user . If the privileges are in the database they will be updated . If the privileges are not in the configuration they will be revoked from user .
fn create_or_update_privileges(
    conn: &mut DbConnection,
    config: &Config,
    dryrun: bool,
) -> Result<()> {
    let mut summary = vec![vec![
        "User".to_string(),
        "Role Name".to_string(),
        "Detail".to_string(),
        "Status".to_string(),
    ]];
    summary.push(vec![
        "---".to_string(),
        "---".to_string(),
        "---".to_string(),
        "---".to_string(),
    ]);
    // Loop through users in config
    // Get the user Role object by the user.roles[*].name
    // Apply the Role sql privileges to the cluster
    for user in &config.users {
        // Compare privileges on config and db
        // If privileges on config are not in db, add them
        // If privileges on db are not in config, remove them
        for role_name in user.roles.iter() {
            let role = config.roles.iter().find(|&r| r.find(role_name)).unwrap();
            // TODO: revoke if privileges on db are not in configuration
            let sql = role.to_sql(&user.name);
            let mut status = if dryrun {
                "dry-run".to_string()
            } else {
                "updated".to_string()
            };
            if !dryrun {
                let nrows = conn.execute(&sql, &[]).unwrap_or_else(|e| {
                    error!("{}: {}", Red.paint("Error"), sql);
                    error!("  -> {}: {}", Red.paint("Error details"), e);
                    status = "error".to_string();
                    -1
                });
                if nrows > -1 {
                    info!(
                        "{}: {} {}",
                        Green.paint("Success"),
                        Purple.paint(sql),
                        format!("(updated {} row(s))", nrows)
                    );
                }
            } else {
                info!("{}: {}", Purple.paint("Dry-run"), sql);
            }
            let detail = match role {
                Role::Database(role) => format!("database{:?}", role.databases.clone()),
                Role::Schema(role) => format!("schema{:?}", role.schemas.clone()),
                Role::Table(role) => format!("table{:?}", role.tables.clone()),
            };
            // Update summary
            summary.push(vec![
                user.name.clone(),
                role_name.clone(),
                detail.to_string(),
                status.to_string(),
            ]);
        }
    }
    // Show summary
    print_summary(summary);
    Ok(())
}
fn create_or_update_privileges ( conn : & mut DbConnection , config : & Config , dryrun : bool , ) -> Result < ( ) > { let mut summary = vec ! [ vec [ " " to_string ( ) " " to_string ( ) " " to_string ( ) " " to_string ( ) ] ] ; summary . push ( vec ! [ " " to_string ( ) " " to_string ( ) " " to_string ( ) " " to_string ( ) ] ) ; // Loop through users in config // Get the user Role object by the user.roles[*].name // Apply the Role sql privileges to the cluster for user in & config . users { // Compare privileges on config and db // If privileges on config are not in db, add them // If privileges on db are not in config, remove them for role_name in user . roles . iter ( ) { let role = config . roles . iter ( ) . find ( | & r | r . find ( role_name ) ) . unwrap ( ) ; // TODO: revoke if privileges on db are not in configuration let sql = role . to_sql ( & user . name ) ; let mut status = if dryrun { " " . to_string ( ) } else { " " . to_string ( ) } ; if ! dryrun { let nrows = conn . execute ( & sql , & [ ] ) . unwrap_or_else ( | e | { error ! ( " " Red paint ( " " ) sql ) ; error ! ( " " Red paint ( " " ) e ) ; status = " " . to_string ( ) ; - 1 } ) ; if nrows > - 1 { info ! ( " " Green paint ( " " ) Purple paint ( sql ) format ( " " nrows ) ) ; } } else { info ! ( " " Purple paint ( " " ) sql ) ; } let detail = match role { Role :: Database ( role ) => format ! ( " " role databases clone ( ) ) , Role :: Schema ( role ) => format ! ( " " role schemas clone ( ) ) , Role :: Table ( role ) => format ! ( " " role tables clone ( ) ) , } ; // Update summary summary . push ( vec ! [ user name clone ( ) role_name clone ( ) detail to_string ( ) status to_string ( ) ] ) ; } } // Show summary print_summary ( summary ) ; Ok ( ( ) ) }
Render role configuration to SQL and sync with database.
Render role configuration to SQL and sync with database .
conn
&mut DbConnection
config
&Config
dryrun
bool
conn
&mut DbConnection
config
&Config
dryrun
bool