-
Notifications
You must be signed in to change notification settings - Fork 122
fix(UI): update user_id to username in ssh/token table #1330
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -46,10 +46,11 @@ pub struct MonoRepo { | |||||
| pub from_hash: String, | ||||||
| pub to_hash: String, | ||||||
| // current_commit only exists when an unpack operation occurs. | ||||||
| // When only a branch is updated and the pack file is empty, this value will be null. | ||||||
| // When only a branch is updated and the pack file is empty, this value will be None. | ||||||
| pub current_commit: Arc<RwLock<Option<Commit>>>, | ||||||
| pub mr_link: Arc<RwLock<Option<String>>>, | ||||||
| pub bellatrix: Arc<Bellatrix>, | ||||||
| pub username: Option<String>, | ||||||
| } | ||||||
|
|
||||||
| #[async_trait] | ||||||
|
|
@@ -311,18 +312,30 @@ impl RepoHandler for MonoRepo { | |||||
| async fn save_or_update_mr(&self) -> Result<(), MegaError> { | ||||||
| let storage = self.storage.mr_storage(); | ||||||
| let path_str = self.path.to_str().unwrap(); | ||||||
| let username = self.username(); | ||||||
|
|
||||||
| match storage.get_open_mr_by_path(path_str).await? { | ||||||
| match storage.get_open_mr_by_path(path_str, &username).await? { | ||||||
| Some(mr) => { | ||||||
| self.update_existing_mr(mr).await?; | ||||||
| } | ||||||
| None => { | ||||||
| let link_guard = self.mr_link.read().await; | ||||||
| let mr_link = link_guard.as_ref().unwrap(); | ||||||
| let commit_guard = self.current_commit.read().await; | ||||||
| let title = commit_guard.as_ref().unwrap().format_message(); | ||||||
| let title = if let Some(commit) = commit_guard.as_ref() { | ||||||
| commit.format_message() | ||||||
| } else { | ||||||
| String::new() | ||||||
| }; | ||||||
| storage | ||||||
| .new_mr(path_str, mr_link, &title, &self.from_hash, &self.to_hash) | ||||||
| .new_mr( | ||||||
| path_str, | ||||||
| mr_link, | ||||||
| &title, | ||||||
| &self.from_hash, | ||||||
| &self.to_hash, | ||||||
| &username, | ||||||
| ) | ||||||
| .await?; | ||||||
| } | ||||||
| }; | ||||||
|
|
@@ -377,7 +390,11 @@ impl MonoRepo { | |||||
| async fn fetch_or_new_mr_link(&self) -> Result<String, MegaError> { | ||||||
| let storage = self.storage.mr_storage(); | ||||||
| let path_str = self.path.to_str().unwrap(); | ||||||
| let mr_link = match storage.get_open_mr_by_path(path_str).await.unwrap() { | ||||||
| let mr_link = match storage | ||||||
| .get_open_mr_by_path(path_str, &self.username()) | ||||||
| .await | ||||||
| .unwrap() | ||||||
| { | ||||||
| Some(mr) => mr.link.clone(), | ||||||
| None => { | ||||||
| if self.from_hash == "0".repeat(40) { | ||||||
|
|
@@ -401,9 +418,10 @@ impl MonoRepo { | |||||
| comment_stg | ||||||
| .add_conversation( | ||||||
| &mr.link, | ||||||
| "", | ||||||
| &self.username(), | ||||||
| Some(format!( | ||||||
| "Mega updated the mr automatic from {} to {}", | ||||||
| "{} updated the mr automatic from {} to {}", | ||||||
| self.username(), | ||||||
| &mr.to_hash[..6], | ||||||
| &self.to_hash[..6] | ||||||
| )), | ||||||
|
|
@@ -415,11 +433,12 @@ impl MonoRepo { | |||||
| tracing::info!("repeat commit with mr: {}, do nothing", mr.id); | ||||||
| } | ||||||
| } else { | ||||||
| // TODO 直接覆盖? | ||||||
| comment_stg | ||||||
| .add_conversation( | ||||||
| &mr.link, | ||||||
| "", | ||||||
| Some("Mega closed MR due to conflict".to_string()), | ||||||
| &self.username(), | ||||||
| Some(format!("{} closed MR due to conflict", self.username(),)), | ||||||
| ConvTypeEnum::Closed, | ||||||
| ) | ||||||
| .await | ||||||
|
|
@@ -510,6 +529,10 @@ impl MonoRepo { | |||||
| let to_tree: Tree = mono_stg.get_tree_by_hash(&to_c.tree).await?.unwrap().into(); | ||||||
| diff_trees(&to_tree, &from_tree) | ||||||
| } | ||||||
|
|
||||||
| pub fn username(&self) -> String { | ||||||
| self.username.clone().unwrap_or(String::from("Admin")) | ||||||
|
||||||
| self.username.clone().unwrap_or(String::from("Admin")) | |
| self.username.clone().unwrap_or(String::from("system")) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,11 @@ | ||
| use core::fmt; | ||
| use std::{path::PathBuf, str::FromStr, sync::Arc}; | ||
|
|
||
| use base64::engine::general_purpose; | ||
| use base64::prelude::*; | ||
| use http::{HeaderMap, HeaderValue}; | ||
| use tokio::sync::RwLock; | ||
|
|
||
| use bellatrix::Bellatrix; | ||
| use callisto::sea_orm_active_enums::RefTypeEnum; | ||
| use common::{ | ||
|
|
@@ -10,7 +15,6 @@ use common::{ | |
| use import_refs::RefCommand; | ||
| use jupiter::storage::Storage; | ||
| use repo::Repo; | ||
| use tokio::sync::RwLock; | ||
|
|
||
| use crate::pack::{import_repo::ImportRepo, monorepo::MonoRepo, RepoHandler}; | ||
|
|
||
|
|
@@ -26,6 +30,7 @@ pub struct SmartProtocol { | |
| pub command_list: Vec<RefCommand>, | ||
| pub service_type: Option<ServiceType>, | ||
| pub storage: Storage, | ||
| pub username: Option<String>, | ||
| } | ||
|
|
||
| #[derive(Debug, PartialEq, Clone, Copy, Default)] | ||
|
|
@@ -134,6 +139,7 @@ impl SmartProtocol { | |
| command_list: Vec::new(), | ||
| service_type: None, | ||
| storage, | ||
| username: None, | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -146,6 +152,7 @@ impl SmartProtocol { | |
| command_list: Vec::new(), | ||
| service_type: None, | ||
| storage, | ||
| username: None, | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -183,6 +190,7 @@ impl SmartProtocol { | |
| current_commit: Arc::new(RwLock::new(None)), | ||
| mr_link: Arc::new(RwLock::new(None)), | ||
| bellatrix: Arc::new(Bellatrix::new(self.storage.config().build.clone())), | ||
| username: self.username.clone(), | ||
| }; | ||
| if let Some(command) = self | ||
| .command_list | ||
|
|
@@ -195,6 +203,45 @@ impl SmartProtocol { | |
| Ok(Arc::new(res)) | ||
| } | ||
| } | ||
|
|
||
| pub fn enable_http_auth(&self) -> bool { | ||
| self.storage.config().enable_http_auth() | ||
| } | ||
|
|
||
| pub async fn http_auth(&mut self, header: &HeaderMap<HeaderValue>) -> bool { | ||
| for (k, v) in header { | ||
| if k == http::header::AUTHORIZATION { | ||
| let decoded = general_purpose::STANDARD | ||
|
||
| .decode( | ||
| v.to_str() | ||
| .unwrap() | ||
| .strip_prefix("Basic ") | ||
| .unwrap() | ||
| .as_bytes(), | ||
| ) | ||
| .unwrap(); | ||
| let credentials = String::from_utf8(decoded).unwrap_or_default(); | ||
| let mut parts = credentials.splitn(2, ':'); | ||
| let username = parts.next().unwrap_or(""); | ||
| self.username = Some(username.to_owned()); | ||
| let token = parts.next().unwrap_or(""); | ||
| let auth_config = self.storage.config().authentication.clone(); | ||
| if auth_config.enable_test_user | ||
| && username == auth_config.test_user_name | ||
| && token == auth_config.test_user_token | ||
| { | ||
| return true; | ||
| } | ||
| return self | ||
| .storage | ||
| .user_storage() | ||
| .check_token(username, token) | ||
| .await | ||
| .unwrap(); | ||
| } | ||
| } | ||
| false | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,68 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| use sea_orm::{DatabaseBackend, Statement}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| use sea_orm_migration::prelude::*; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #[derive(DeriveMigrationName)] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| pub struct Migration; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #[async_trait::async_trait] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| impl MigrationTrait for Migration { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let backend = manager.get_database_backend(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| match backend { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| DatabaseBackend::Postgres | DatabaseBackend::MySql => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| manager | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .alter_table( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Table::alter() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .table(MegaMr::Table) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .add_column_if_not_exists( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ColumnDef::new(Alias::new("username")) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .string() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .not_null() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .default(""), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .to_owned(), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .await?; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| manager | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .get_connection() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .execute(Statement::from_string( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| manager.get_database_backend(), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "ALTER TABLE access_token RENAME COLUMN user_id TO username".to_owned(), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| )) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .await?; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| manager | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .get_connection() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .execute(Statement::from_string( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| manager.get_database_backend(), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "ALTER TABLE ssh_keys RENAME COLUMN user_id TO username".to_owned(), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| )) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .await?; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| DatabaseBackend::Sqlite => {} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| DatabaseBackend::Sqlite => {} | |
| DatabaseBackend::Sqlite => { | |
| // Add 'username' column to mega_mr table if it does not exist | |
| manager | |
| .get_connection() | |
| .execute(Statement::from_string( | |
| manager.get_database_backend(), | |
| "ALTER TABLE mega_mr ADD COLUMN username TEXT NOT NULL DEFAULT ''".to_owned(), | |
| )) | |
| .await?; | |
| // Rename 'user_id' to 'username' in access_token table | |
| manager | |
| .get_connection() | |
| .execute(Statement::from_string( | |
| manager.get_database_backend(), | |
| "ALTER TABLE access_token RENAME COLUMN user_id TO username".to_owned(), | |
| )) | |
| .await?; | |
| // Rename 'user_id' to 'username' in ssh_keys table | |
| manager | |
| .get_connection() | |
| .execute(Statement::from_string( | |
| manager.get_database_backend(), | |
| "ALTER TABLE ssh_keys RENAME COLUMN user_id TO username".to_owned(), | |
| )) | |
| .await?; | |
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comment is in Chinese and appears to be a TODO item asking about direct overwriting. Comments should be in English for consistency and the TODO should be clarified or addressed.