refactor: no longer set username and userToken as member variables in user class and get them only by global state
This commit is contained in:
		
							
								
								
									
										36
									
								
								src/user.ts
									
									
									
									
									
								
							
							
						
						
									
										36
									
								
								src/user.ts
									
									
									
									
									
								
							@@ -12,12 +12,11 @@ const sshpk = require('sshpk');
 | 
			
		||||
 | 
			
		||||
export default class User {
 | 
			
		||||
  private context: vscode.ExtensionContext;
 | 
			
		||||
  private username: string | undefined;
 | 
			
		||||
  private userToken: string | undefined;
 | 
			
		||||
  private usernameKey: string = 'devstarUsername'
 | 
			
		||||
  private userTokenKey: string = 'devstarUserToken'
 | 
			
		||||
  // 修改devstarDomain会影响hostname, username key, user token key
 | 
			
		||||
  private devstarDomain: string;
 | 
			
		||||
  private devstarHostname: string;
 | 
			
		||||
  private usernameKey: string; // devstarUsername_<devstarDomain>
 | 
			
		||||
  private userTokenKey: string; // devstarUserToken_<devstarDomain>
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * devstarDomain由配置项提供
 | 
			
		||||
@@ -34,8 +33,6 @@ export default class User {
 | 
			
		||||
 | 
			
		||||
  constructor(context: vscode.ExtensionContext, devstarDomain?: string) {
 | 
			
		||||
    this.context = context;
 | 
			
		||||
    this.username = this.context.globalState.get(this.usernameKey);
 | 
			
		||||
    this.userToken = this.context.globalState.get(this.userTokenKey);
 | 
			
		||||
 | 
			
		||||
    // 提取devstar domain的主域名,用于本地ssh key的命名
 | 
			
		||||
    if (devstarDomain != undefined && devstarDomain != "") {
 | 
			
		||||
@@ -66,6 +63,8 @@ export default class User {
 | 
			
		||||
      this.devstarDomain = devstarDomain.endsWith('/') ? devstarDomain.slice(0, -1) : devstarDomain
 | 
			
		||||
      const parsedUrl = new URL(devstarDomain)
 | 
			
		||||
      this.devstarHostname = parsedUrl.hostname.replace(/\./g, '_');
 | 
			
		||||
      this.usernameKey = "devstarUsername_" + this.devstarDomain
 | 
			
		||||
      this.userTokenKey = "devstarUserToken_" + this.devstarDomain
 | 
			
		||||
    } else {
 | 
			
		||||
      console.error(vscode.l10n.t("devstar domain is null"))
 | 
			
		||||
    }
 | 
			
		||||
@@ -133,16 +132,15 @@ export default class User {
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  public getUsernameFromLocal(): string | undefined {
 | 
			
		||||
    return this.username;
 | 
			
		||||
    return this.context.globalState.get(this.usernameKey);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  public getUserTokenFromLocal(): string | undefined {
 | 
			
		||||
    return this.userToken;
 | 
			
		||||
    return this.context.globalState.get(this.userTokenKey);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  public setUsernameToLocal(username: string) {
 | 
			
		||||
    this.context.globalState.update(this.usernameKey, username);
 | 
			
		||||
    this.username = username;
 | 
			
		||||
    if (username !== "") {
 | 
			
		||||
      console.log('Username has been stored.')
 | 
			
		||||
    } else {
 | 
			
		||||
@@ -152,7 +150,6 @@ export default class User {
 | 
			
		||||
 | 
			
		||||
  public setUserTokenToLocal(userToken: string) {
 | 
			
		||||
    this.context.globalState.update(this.userTokenKey, userToken)
 | 
			
		||||
    this.userToken = userToken
 | 
			
		||||
    if (userToken !== "") {
 | 
			
		||||
      console.log('Token has been stored.');
 | 
			
		||||
    } else {
 | 
			
		||||
@@ -169,19 +166,23 @@ export default class User {
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  public getUserPrivateKeyPath(): string {
 | 
			
		||||
    if (!this.isLogged) {
 | 
			
		||||
    if (!this.isLogged()) {
 | 
			
		||||
      return '';
 | 
			
		||||
    } else {
 | 
			
		||||
      const username: string|undefined  = this.context.globalState.get(this.usernameKey)
 | 
			
		||||
      // islogged为true,username不为空
 | 
			
		||||
      return path.join(os.homedir(), '.ssh', `id_rsa_${username}_${this.devstarHostname}`)
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return path.join(os.homedir(), '.ssh', `id_rsa_${this.username}_${this.devstarHostname}`)
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  public getUserPublicKeyPath(): string {
 | 
			
		||||
    if (!this.isLogged) {
 | 
			
		||||
    if (!this.isLogged()) {
 | 
			
		||||
      return '';
 | 
			
		||||
    } else {
 | 
			
		||||
      const username: string|undefined  = this.context.globalState.get(this.usernameKey)
 | 
			
		||||
      // islogged为true,username不为空
 | 
			
		||||
      return path.join(os.homedir(), '.ssh', `id_rsa_${username}_${this.devstarHostname}.pub`)
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return path.join(os.homedir(), '.ssh', `id_rsa_${this.username}_${this.devstarHostname}.pub`)
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  public existUserPublicKey(): boolean {
 | 
			
		||||
@@ -249,7 +250,8 @@ export default class User {
 | 
			
		||||
      this.updateLocalUserPrivateKeyPath(this.getUserPrivateKeyPath())
 | 
			
		||||
      console.log(`Update local user private key path.`)
 | 
			
		||||
    } catch (error) {
 | 
			
		||||
      console.error(`Failed to write public/private key into the user(${this.username}) ssh public/key file: `, error);
 | 
			
		||||
      const username: string|undefined =  this.context.globalState.get(this.usernameKey)
 | 
			
		||||
      console.error(`Failed to write public/private key into the user(${username}) ssh public/key file: `, error);
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user