Skip to content

Commit

Permalink
♻ refactor: mask sensitive info (#729)
Browse files Browse the repository at this point in the history
  • Loading branch information
wzh425 authored Aug 21, 2024
1 parent 9afba13 commit 733f8dc
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
46 changes: 46 additions & 0 deletions src/Masa.Stack.Components/Extensions/TextFormattingExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
namespace Masa.Stack.Components.Extensions;

public static class TextFormattingExtensions
{
public static string MaskPhoneNumber(this string phoneNumber)
{
if (phoneNumber.Length == 11)
{
return phoneNumber.Substring(0, 3) + "****" + phoneNumber.Substring(7);
}
return phoneNumber;
}

public static string MaskIdCard(this string idCard)
{
if (idCard.Length == 18)
{
return idCard.Substring(0, 6) + "********" + idCard.Substring(14);
}
return idCard;
}

public static string MaskAccount(this string account)
{
if (account.Length > 2)
{
return account.Substring(0, 1) + new string('*', account.Length - 2) + account.Substring(account.Length - 1);
}
else if (account.Length == 2)
{
return account.Substring(0, 1) + "*";
}
return account;
}

public static string MaskSensitiveInfo(this string input, string type)
{
return type switch
{
"phone" => input.MaskPhoneNumber(),
"idCard" => input.MaskIdCard(),
"account" => input.MaskAccount(),
_ => input
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@
Clearable=Clearable
PrependInnerIcon="mdi-magnify">
<ItemContent Context="data">
@RenderFragments.UserSelectItem(data.Item.Avatar, data.Item.DisplayName, data.Item.PhoneNumber, data.Item.Account, data.Item.Email)
@RenderFragments.UserSelectItem(data.Item.Avatar, data.Item.DisplayName, data.Item.PhoneNumber?.MaskPhoneNumber(), data.Item.Account.MaskAccount(), data.Item.Email)
</ItemContent>
</SAutoComplete>
2 changes: 1 addition & 1 deletion tests/MasaWebApp/Shared/MainLayout.razor
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
@using Masa.Stack.Components.Models
@inherits LayoutComponentBase

<SLayout AppId="auth-web-dev" OnSignOut="SignOut" WhiteUris="_whiteUris" TeamRouteFormat="team/{0}" IsShowEnvironmentSwitch=true
<SLayout AppId="lonsid-iot-admin" OnSignOut="SignOut" WhiteUris="_whiteUris" TeamRouteFormat="team/{0}" IsShowEnvironmentSwitch=true
Logo="https://cdn.masastack.com/stack/images/logo/MASAStack/logo-h-en.png"
MiniLogo="https://cdn.masastack.com/stack/images/logo/MASAStack/logo.png">
@Body
Expand Down

0 comments on commit 733f8dc

Please sign in to comment.