feat: move User.PhonePrefix to Organization.PhonePrefix

Signed-off-by: Kininaru <shiftregister233@outlook.com>
This commit is contained in:
Kininaru
2021-05-13 09:39:07 +08:00
parent 827930a020
commit 892cb39e3e
11 changed files with 63 additions and 35 deletions

View File

@ -101,7 +101,6 @@ func (c *ApiController) Signup() {
DisplayName: form.Name,
Avatar: "https://casbin.org/img/casbin.svg",
Email: form.Email,
PhonePrefix: form.PhonePrefix,
Phone: form.Phone,
Affiliation: form.Affiliation,
IsAdmin: false,

View File

@ -15,12 +15,24 @@
package controllers
import (
"fmt"
"strings"
"github.com/casdoor/casdoor/object"
)
func (c *ApiController) SendVerificationCode() {
userId := c.GetSessionUser()
if len(userId) == 0 {
c.ResponseError("Please sign in first")
return
}
user := object.GetUser(userId)
if user == nil {
c.ResponseError("No such user.")
return
}
destType := c.Ctx.Request.Form.Get("type")
dest := c.Ctx.Request.Form.Get("dest")
remoteAddr := c.Ctx.Request.RemoteAddr
@ -37,6 +49,12 @@ func (c *ApiController) SendVerificationCode() {
case "email":
ret = object.SendVerificationCodeToEmail(remoteAddr, dest)
case "phone":
org := object.GetOrganizationByName(user.Owner)
phonePrefix := "86"
if org != nil && org.PhonePrefix != "" {
phonePrefix = org.PhonePrefix
}
dest = fmt.Sprintf("+%s%s", phonePrefix, dest)
ret = object.SendVerificationCodeToPhone(remoteAddr, dest)
}
@ -71,7 +89,16 @@ func (c *ApiController) ResetEmailOrPhone() {
return
}
if ret := object.CheckVerificationCode(dest, code); len(ret) != 0 {
checkDest := dest
if destType == "phone" {
org := object.GetOrganizationByName(user.Owner)
phonePrefix := "86"
if org != nil && org.PhonePrefix != "" {
phonePrefix = org.PhonePrefix
}
checkDest = fmt.Sprintf("+%s%s", phonePrefix, dest)
}
if ret := object.CheckVerificationCode(checkDest, code); len(ret) != 0 {
c.ResponseError(ret)
return
}
@ -81,15 +108,8 @@ func (c *ApiController) ResetEmailOrPhone() {
user.Email = dest
object.SetUserField(user, "email", user.Email)
case "phone":
if strings.Index(dest, "+86") == 0 {
user.PhonePrefix = "86"
user.Phone = dest[3:]
} else if strings.Index(dest, "+1") == 0 {
user.PhonePrefix = "1"
user.Phone = dest[2:]
}
user.Phone = dest
object.SetUserField(user, "phone", user.Phone)
object.SetUserField(user, "phone_prefix", user.PhonePrefix)
default:
c.ResponseError("Unknown type.")
return

View File

@ -29,6 +29,7 @@ type Organization struct {
Favicon string `xorm:"varchar(100)" json:"favicon"`
PasswordType string `xorm:"varchar(100)" json:"passwordType"`
PasswordSalt string `xorm:"varchar(100)" json:"passwordSalt"`
PhonePrefix string `xorm:"varchar(10)" json:"phonePrefix"`
}
func GetOrganizations(owner string) []*Organization {
@ -91,3 +92,16 @@ func DeleteOrganization(organization *Organization) bool {
return affected != 0
}
func GetOrganizationByName(name string) *Organization {
var ret Organization
ret.Name = name
has, err := adapter.Engine.Get(&ret)
if err != nil {
panic(err)
}
if !has {
return nil
}
return &ret
}

View File

@ -34,7 +34,6 @@ type User struct {
DisplayName string `xorm:"varchar(100)" json:"displayName"`
Avatar string `xorm:"varchar(255)" json:"avatar"`
Email string `xorm:"varchar(100)" json:"email"`
PhonePrefix string `xorm:"varchar(10)" json:"phonePrefix"`
Phone string `xorm:"varchar(100)" json:"phone"`
Affiliation string `xorm:"varchar(100)" json:"affiliation"`
Tag string `xorm:"varchar(100)" json:"tag"`

View File

@ -149,6 +149,16 @@ class OrganizationEditPage extends React.Component {
}} />
</Col>
</Row>
<Row style={{marginTop: '20px'}} >
<Col style={{marginTop: '5px'}} span={2}>
{i18next.t("general:Phone Prefix")}:
</Col>
<Col span={22} >
<Input addonBefore={"+"} value={this.state.organization.phonePrefix} onChange={e => {
this.updateOrganizationField('phonePrefix', e.target.value);
}} />
</Col>
</Row>
</Card>
)
}

View File

@ -37,7 +37,7 @@ export const ResetModal = (props) => {
let dest = document.getElementById("dest").value;
let code = document.getElementById("code").value;
if (dest === "") {
Setting.showMessage("error", i18next.t("user:Empty ") + destType);
Setting.showMessage("error", i18next.t("user:Empty " + destType));
return;
}
if (code === "") {
@ -70,7 +70,7 @@ export const ResetModal = (props) => {
if (sendCodeCoolDown) return;
let dest = document.getElementById("dest").value;
if (dest === "") {
Setting.showMessage("error", i18next.t("user:Empty ") + destType);
Setting.showMessage("error", i18next.t("user:Empty " + destType));
return;
}
UserBackend.sendCode(dest, destType).then(res => {
@ -86,7 +86,7 @@ export const ResetModal = (props) => {
let placeHolder = "";
if (destType === "email") placeHolder = i18next.t("user:Input your email");
else if (destType === "phone") placeHolder = i18next.t("user:Phone prefix is needed");
else if (destType === "phone") placeHolder = i18next.t("user:Input your phone number");
return (
<Row>

View File

@ -285,7 +285,7 @@ class UserEditPage extends React.Component {
{i18next.t("general:Phone")}:
</Col>
<Col span={22} >
<Input value={"+" + this.state.user.phonePrefix + this.state.user.phone} disabled/>
<Input value={this.state.user.phone} disabled/>
{ this.state.user.id === this.props.account.id ? (<ResetModal buttonText={i18next.t("user:Reset Phone")} destType={"phone"} coolDownTime={60}/>) : null}
</Col>
</Row>

View File

@ -52,7 +52,6 @@ class UserListPage extends React.Component {
displayName: `New User - ${this.state.users.length}`,
avatar: "https://casbin.org/img/casbin.svg",
email: "user@example.com",
phonePrefix: "86",
phone: "12345678",
affiliation: "Example Inc.",
tag: "staff",

View File

@ -133,19 +133,6 @@ class SignupPage extends React.Component {
)
}
const prefixSelector = (
<Form.Item name="phonePrefix" noStyle>
<Select
style={{
width: 80,
}}
>
<Option value="1">+1</Option>
<Option value="86">+86</Option>
</Select>
</Form.Item>
);
return (
<Form
{...formItemLayout}
@ -156,7 +143,6 @@ class SignupPage extends React.Component {
initialValues={{
application: application.name,
organization: application.organization,
phonePrefix: '86',
}}
style={{width: !Setting.isMobile() ? "400px" : "250px"}}
size="large"
@ -285,7 +271,6 @@ class SignupPage extends React.Component {
]}
>
<Input
addonBefore={prefixSelector}
style={{
width: '100%',
}}

View File

@ -29,7 +29,8 @@
"Users under all organizations": "Users under all organizations",
"OAuth providers": "OAuth providers",
"Applications that requires authentication": "Applications that requires authentication",
"Swagger": "Swagger"
"Swagger": "Swagger",
"Phone Prefix": "Phone Prefix"
},
"signup":
{
@ -126,7 +127,7 @@
"email reset": "Email Reset",
"Code Sent": "Code Sent",
"Input your email": "Input your email",
"Phone prefix is needed": "Phone prefix is needed",
"Input your phone number": "Input your phone number",
"New phone": "New Phone",
"New email": "New Email",
"Code You Received": "Code You Received",

View File

@ -29,7 +29,8 @@
"Users under all organizations": "所有组织里的用户",
"OAuth providers": "OAuth提供方",
"Applications that requires authentication": "需要鉴权的应用",
"Swagger": "API总览"
"Swagger": "API总览",
"Phone Prefix": "电话前缀"
},
"signup":
{
@ -128,7 +129,7 @@
"email reset": "邮箱已设置",
"Code Sent": "验证码已发送",
"Input your email": "请输入邮箱",
"Phone prefix is needed": "记得输入地区前缀(+86",
"Input your phone number": "输入电话号码",
"New phone": "新的电话号",
"New email": "新的邮箱",
"Code You Received": "你收到的验证码",