mirror of
https://github.com/YGGverse/Yoda.git
synced 2025-01-30 13:04:13 +00:00
implement existing certificate selection
This commit is contained in:
parent
10cf4fc6a1
commit
728a9c06d5
@ -57,18 +57,18 @@ impl Gemini {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Init events
|
// Init events
|
||||||
widget.connect_response({
|
widget.on_apply(move |response| match response {
|
||||||
let profile = profile.clone();
|
// Apply selected identity for `auth_uri`
|
||||||
move |value| {
|
Some(profile_identity_gemini_id) => {
|
||||||
match value {
|
profile
|
||||||
Some(id) => {
|
.identity
|
||||||
// Activate selected identity ID
|
.gemini
|
||||||
}
|
.auth
|
||||||
None => {
|
.apply(profile_identity_gemini_id, auth_uri.to_string().as_str())
|
||||||
// Create and select new identity
|
.unwrap(); //@TODO handle errors
|
||||||
}
|
|
||||||
} // @TODO handle result
|
|
||||||
}
|
}
|
||||||
|
// Create new certificate, then apply it to the new identity for `auth_uri`
|
||||||
|
None => {}
|
||||||
});
|
});
|
||||||
|
|
||||||
// Return activated `Self`
|
// Return activated `Self`
|
||||||
|
@ -62,15 +62,12 @@ impl Widget {
|
|||||||
|
|
||||||
// Actions
|
// Actions
|
||||||
|
|
||||||
/// Wrapper for default [response](https://gnome.pages.gitlab.gnome.org/libadwaita/doc/main/signal.AlertDialog.response.html) signal
|
/// Callback wrapper for `apply` [response](https://gnome.pages.gitlab.gnome.org/libadwaita/doc/main/signal.AlertDialog.response.html)
|
||||||
/// * return `profile_identity_gemini_id` or new record request on `None`
|
/// * return `profile_identity_gemini_id` or new record request on `None`
|
||||||
pub fn connect_response(&self, callback: impl Fn(Option<i64>) + 'static) {
|
pub fn on_apply(&self, callback: impl Fn(Option<i64>) + 'static) {
|
||||||
self.gobject.connect_response(None, move |_, response| {
|
self.gobject.connect_response(Some(RESPONSE_APPLY.0), {
|
||||||
if response == RESPONSE_APPLY.0 {
|
let form = self.form.clone();
|
||||||
callback(None)
|
move |_, _| callback(form.list.selected())
|
||||||
} else {
|
|
||||||
callback(None)
|
|
||||||
} // @TODO
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -32,7 +32,7 @@ impl Form {
|
|||||||
gobject.append(&name.gobject);
|
gobject.append(&name.gobject);
|
||||||
|
|
||||||
// Connect events
|
// Connect events
|
||||||
list.connect_selected_notify(move |key| name.gobject.set_visible(key.is_none()));
|
list.on_select(move |key| name.gobject.set_visible(key.is_none()));
|
||||||
|
|
||||||
// Return activated `Self`
|
// Return activated `Self`
|
||||||
Self {
|
Self {
|
||||||
|
@ -59,8 +59,8 @@ impl List {
|
|||||||
// Events
|
// Events
|
||||||
|
|
||||||
/// Run callback function on `connect_selected_notify` event
|
/// Run callback function on `connect_selected_notify` event
|
||||||
/// * return formatted `profile_identity_gemini_id` match selected
|
/// * return formatted `profile_identity_gemini_id` match selected item
|
||||||
pub fn connect_selected_notify(&self, callback: impl Fn(Option<i64>) + 'static) {
|
pub fn on_select(&self, callback: impl Fn(Option<i64>) + 'static) {
|
||||||
self.gobject.connect_selected_notify(move |list| {
|
self.gobject.connect_selected_notify(move |list| {
|
||||||
callback(
|
callback(
|
||||||
list.selected_item()
|
list.selected_item()
|
||||||
@ -70,4 +70,15 @@ impl List {
|
|||||||
)
|
)
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Getters
|
||||||
|
|
||||||
|
/// Get formatted `profile_identity_gemini_id` **option** match selected item
|
||||||
|
pub fn selected(&self) -> Option<i64> {
|
||||||
|
self.gobject
|
||||||
|
.selected_item()
|
||||||
|
.and_downcast::<Item>()
|
||||||
|
.unwrap()
|
||||||
|
.profile_identity_gemini_id_option()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -18,6 +18,7 @@ pub struct Gemini {
|
|||||||
pub auth: Rc<Auth>,
|
pub auth: Rc<Auth>,
|
||||||
pub database: Rc<Database>,
|
pub database: Rc<Database>,
|
||||||
pub memory: Rc<Memory>,
|
pub memory: Rc<Memory>,
|
||||||
|
profile_identity_id: Rc<i64>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Gemini {
|
impl Gemini {
|
||||||
@ -33,7 +34,7 @@ impl Gemini {
|
|||||||
Ok(auth) => Rc::new(auth),
|
Ok(auth) => Rc::new(auth),
|
||||||
Err(_) => return Err(Error::AuthInit), // @TODO
|
Err(_) => return Err(Error::AuthInit), // @TODO
|
||||||
};
|
};
|
||||||
let database = Rc::new(Database::new(connection, profile_identity_id));
|
let database = Rc::new(Database::new(connection, profile_identity_id.clone()));
|
||||||
let memory = Rc::new(Memory::new());
|
let memory = Rc::new(Memory::new());
|
||||||
|
|
||||||
// Init `Self`
|
// Init `Self`
|
||||||
@ -41,6 +42,7 @@ impl Gemini {
|
|||||||
auth,
|
auth,
|
||||||
database,
|
database,
|
||||||
memory,
|
memory,
|
||||||
|
profile_identity_id,
|
||||||
};
|
};
|
||||||
|
|
||||||
// Build initial index
|
// Build initial index
|
||||||
@ -69,8 +71,6 @@ impl Gemini {
|
|||||||
};
|
};
|
||||||
Ok(()) // @TODO
|
Ok(()) // @TODO
|
||||||
}
|
}
|
||||||
|
|
||||||
// @TODO create new identity API
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Tools
|
// Tools
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
//! Controller for children `database` and `memory` components
|
||||||
|
|
||||||
mod database;
|
mod database;
|
||||||
mod error;
|
mod error;
|
||||||
mod memory;
|
mod memory;
|
||||||
@ -35,6 +37,45 @@ impl Auth {
|
|||||||
|
|
||||||
// Actions
|
// Actions
|
||||||
|
|
||||||
|
/// Apply `profile_identity_gemini_id` certificate as the auth for `url`
|
||||||
|
/// * deactivate active auth by remove previous records from `Self` database
|
||||||
|
/// * reindex `Self` memory index on success
|
||||||
|
/// * return last insert `profile_identity_gemini_auth_id` on success
|
||||||
|
pub fn apply(&self, profile_identity_gemini_id: i64, url: &str) -> Result<i64, Error> {
|
||||||
|
// Get all records match request
|
||||||
|
match self.database.records(Some(url)) {
|
||||||
|
Ok(records) => {
|
||||||
|
// Cleanup records match `profile_identity_gemini_id` (unauth)
|
||||||
|
for record in records {
|
||||||
|
if record.profile_identity_gemini_id == profile_identity_gemini_id {
|
||||||
|
if self.database.delete(record.id).is_err() {
|
||||||
|
return Err(Error::DatabaseRecordDelete(record.id));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create new record (auth)
|
||||||
|
let profile_identity_gemini_auth_id =
|
||||||
|
match self.database.add(profile_identity_gemini_id, url) {
|
||||||
|
Ok(id) => id,
|
||||||
|
Err(_) => {
|
||||||
|
return Err(Error::DatabaseRecordCreate(
|
||||||
|
profile_identity_gemini_id,
|
||||||
|
url.to_string(),
|
||||||
|
))
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Reindex
|
||||||
|
self.index()?;
|
||||||
|
|
||||||
|
// Done
|
||||||
|
Ok(profile_identity_gemini_auth_id)
|
||||||
|
}
|
||||||
|
Err(_) => return Err(Error::DatabaseRecordsRead(url.to_string())),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Create new `Memory` index from `Database` for `Self`
|
/// Create new `Memory` index from `Database` for `Self`
|
||||||
pub fn index(&self) -> Result<(), Error> {
|
pub fn index(&self) -> Result<(), Error> {
|
||||||
// Clear previous records
|
// Clear previous records
|
||||||
@ -44,14 +85,12 @@ impl Auth {
|
|||||||
match self.database.records(None) {
|
match self.database.records(None) {
|
||||||
Ok(records) => {
|
Ok(records) => {
|
||||||
for record in records {
|
for record in records {
|
||||||
if record.is_active {
|
if self
|
||||||
if self
|
.memory
|
||||||
.memory
|
.add(record.url, record.profile_identity_gemini_id)
|
||||||
.add(record.url, record.profile_identity_gemini_id)
|
.is_err()
|
||||||
.is_err()
|
{
|
||||||
{
|
return Err(Error::MemoryIndex);
|
||||||
return Err(Error::MemoryIndex);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,9 +2,8 @@ use sqlite::{Connection, Error, Transaction};
|
|||||||
use std::{rc::Rc, sync::RwLock};
|
use std::{rc::Rc, sync::RwLock};
|
||||||
|
|
||||||
pub struct Table {
|
pub struct Table {
|
||||||
//pub id: i64,
|
pub id: i64,
|
||||||
pub profile_identity_gemini_id: i64,
|
pub profile_identity_gemini_id: i64,
|
||||||
pub is_active: bool,
|
|
||||||
pub url: String,
|
pub url: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -21,6 +20,43 @@ impl Database {
|
|||||||
Self { connection }
|
Self { connection }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Actions
|
||||||
|
|
||||||
|
/// Create new record in database
|
||||||
|
pub fn add(&self, profile_identity_gemini_id: i64, url: &str) -> Result<i64, Error> {
|
||||||
|
// Begin new transaction
|
||||||
|
let mut writable = self.connection.write().unwrap(); // @TODO
|
||||||
|
let tx = writable.transaction()?;
|
||||||
|
|
||||||
|
// Create new record
|
||||||
|
insert(&tx, profile_identity_gemini_id, url)?;
|
||||||
|
|
||||||
|
// Hold insert ID for result
|
||||||
|
let id = last_insert_id(&tx);
|
||||||
|
|
||||||
|
// Done
|
||||||
|
match tx.commit() {
|
||||||
|
Ok(_) => Ok(id),
|
||||||
|
Err(reason) => Err(reason),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Delete record with given `id` from database
|
||||||
|
pub fn delete(&self, id: i64) -> Result<(), Error> {
|
||||||
|
// Begin new transaction
|
||||||
|
let mut writable = self.connection.write().unwrap(); // @TODO
|
||||||
|
let tx = writable.transaction()?;
|
||||||
|
|
||||||
|
// Create new record
|
||||||
|
delete(&tx, id)?;
|
||||||
|
|
||||||
|
// Done
|
||||||
|
match tx.commit() {
|
||||||
|
Ok(_) => Ok(()),
|
||||||
|
Err(reason) => Err(reason),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Getters
|
// Getters
|
||||||
|
|
||||||
/// Get records from database match current `profile_id` optionally filtered by `url`
|
/// Get records from database match current `profile_id` optionally filtered by `url`
|
||||||
@ -39,14 +75,12 @@ pub fn init(tx: &Transaction) -> Result<usize, Error> {
|
|||||||
(
|
(
|
||||||
`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
|
`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
|
||||||
`profile_identity_gemini_id` INTEGER NOT NULL,
|
`profile_identity_gemini_id` INTEGER NOT NULL,
|
||||||
`is_active` INTEGER NOT NULL,
|
|
||||||
`url` VARCHAR(1024) NOT NULL,
|
`url` VARCHAR(1024) NOT NULL,
|
||||||
|
|
||||||
FOREIGN KEY (`profile_identity_gemini_id`) REFERENCES `profile_identity_gemini`(`id`),
|
FOREIGN KEY (`profile_identity_gemini_id`) REFERENCES `profile_identity_gemini`(`id`),
|
||||||
|
|
||||||
UNIQUE (
|
UNIQUE (
|
||||||
`profile_identity_gemini_id`,
|
`profile_identity_gemini_id`,
|
||||||
`is_active`,
|
|
||||||
`url`
|
`url`
|
||||||
)
|
)
|
||||||
)",
|
)",
|
||||||
@ -54,11 +88,31 @@ pub fn init(tx: &Transaction) -> Result<usize, Error> {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn insert(
|
||||||
|
tx: &Transaction,
|
||||||
|
profile_identity_gemini_id: i64,
|
||||||
|
url: &str,
|
||||||
|
) -> Result<usize, Error> {
|
||||||
|
tx.execute(
|
||||||
|
"INSERT INTO `profile_identity_gemini_auth` (
|
||||||
|
`profile_identity_gemini_id`,
|
||||||
|
`url`
|
||||||
|
) VALUES (?, ?)",
|
||||||
|
(profile_identity_gemini_id, url),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn delete(tx: &Transaction, id: i64) -> Result<usize, Error> {
|
||||||
|
tx.execute(
|
||||||
|
"DELETE FROM `profile_identity_gemini_auth` WHERE `id` = ?",
|
||||||
|
[id],
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
pub fn select(tx: &Transaction, url: Option<&str>) -> Result<Vec<Table>, Error> {
|
pub fn select(tx: &Transaction, url: Option<&str>) -> Result<Vec<Table>, Error> {
|
||||||
let mut stmt = tx.prepare(
|
let mut stmt = tx.prepare(
|
||||||
"SELECT `id`,
|
"SELECT `id`,
|
||||||
`profile_identity_gemini_id`,
|
`profile_identity_gemini_id`,
|
||||||
`is_active`,
|
|
||||||
`url`
|
`url`
|
||||||
|
|
||||||
FROM `profile_identity_gemini_auth`
|
FROM `profile_identity_gemini_auth`
|
||||||
@ -67,10 +121,9 @@ pub fn select(tx: &Transaction, url: Option<&str>) -> Result<Vec<Table>, Error>
|
|||||||
|
|
||||||
let result = stmt.query_map([url.unwrap_or("%")], |row| {
|
let result = stmt.query_map([url.unwrap_or("%")], |row| {
|
||||||
Ok(Table {
|
Ok(Table {
|
||||||
//id: row.get(0)?,
|
id: row.get(0)?,
|
||||||
profile_identity_gemini_id: row.get(1)?,
|
profile_identity_gemini_id: row.get(1)?,
|
||||||
is_active: row.get(2)?,
|
url: row.get(2)?,
|
||||||
url: row.get(3)?,
|
|
||||||
})
|
})
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
@ -83,3 +136,7 @@ pub fn select(tx: &Transaction, url: Option<&str>) -> Result<Vec<Table>, Error>
|
|||||||
|
|
||||||
Ok(records)
|
Ok(records)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn last_insert_id(tx: &Transaction) -> i64 {
|
||||||
|
tx.last_insert_rowid()
|
||||||
|
}
|
||||||
|
@ -1,5 +1,8 @@
|
|||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum Error {
|
pub enum Error {
|
||||||
DatabaseIndex,
|
DatabaseIndex,
|
||||||
|
DatabaseRecordCreate(i64, String),
|
||||||
|
DatabaseRecordDelete(i64),
|
||||||
|
DatabaseRecordsRead(String),
|
||||||
MemoryIndex,
|
MemoryIndex,
|
||||||
}
|
}
|
||||||
|
@ -27,6 +27,27 @@ impl Database {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Actions
|
||||||
|
|
||||||
|
/// Create new record in database
|
||||||
|
pub fn add(&self, pem: &str, name: Option<&str>) -> Result<i64, Error> {
|
||||||
|
// Begin new transaction
|
||||||
|
let mut writable = self.connection.write().unwrap(); // @TODO
|
||||||
|
let tx = writable.transaction()?;
|
||||||
|
|
||||||
|
// Create new record
|
||||||
|
insert(&tx, *self.profile_identity_id, pem, name)?;
|
||||||
|
|
||||||
|
// Hold insert ID for result
|
||||||
|
let id = last_insert_id(&tx);
|
||||||
|
|
||||||
|
// Done
|
||||||
|
match tx.commit() {
|
||||||
|
Ok(_) => Ok(id),
|
||||||
|
Err(reason) => Err(reason),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Get all records match current `profile_identity_id`
|
/// Get all records match current `profile_identity_id`
|
||||||
pub fn records(&self) -> Result<Vec<Table>, Error> {
|
pub fn records(&self) -> Result<Vec<Table>, Error> {
|
||||||
let readable = self.connection.read().unwrap(); // @TODO
|
let readable = self.connection.read().unwrap(); // @TODO
|
||||||
@ -56,6 +77,22 @@ pub fn init(tx: &Transaction) -> Result<usize, Error> {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn insert(
|
||||||
|
tx: &Transaction,
|
||||||
|
profile_identity_id: i64,
|
||||||
|
pem: &str,
|
||||||
|
name: Option<&str>,
|
||||||
|
) -> Result<usize, Error> {
|
||||||
|
tx.execute(
|
||||||
|
"INSERT INTO `profile_identity_gemini` (
|
||||||
|
`profile_identity_id`,
|
||||||
|
`pem`,
|
||||||
|
`name`
|
||||||
|
) VALUES (?, ?, ?)",
|
||||||
|
(profile_identity_id, pem, name),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
pub fn select(tx: &Transaction, profile_identity_id: i64) -> Result<Vec<Table>, Error> {
|
pub fn select(tx: &Transaction, profile_identity_id: i64) -> Result<Vec<Table>, Error> {
|
||||||
let mut stmt = tx.prepare(
|
let mut stmt = tx.prepare(
|
||||||
"SELECT `id`,
|
"SELECT `id`,
|
||||||
@ -84,3 +121,7 @@ pub fn select(tx: &Transaction, profile_identity_id: i64) -> Result<Vec<Table>,
|
|||||||
|
|
||||||
Ok(records)
|
Ok(records)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn last_insert_id(tx: &Transaction) -> i64 {
|
||||||
|
tx.last_insert_rowid()
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user