Fix login.

This commit is contained in:
Yang Luo 2021-02-13 00:11:12 +08:00
parent d30c96038d
commit cdc1445883
9 changed files with 99 additions and 26 deletions

View File

@ -2,6 +2,7 @@ package controllers
import (
"encoding/json"
"fmt"
"github.com/casdoor/casdoor/object"
"github.com/casdoor/casdoor/util"
@ -92,16 +93,17 @@ func (c *ApiController) Login() {
panic(err)
}
user, password := form.Username, form.Password
msg := object.CheckUserLogin(user, password)
userId := fmt.Sprintf("%s/%s", form.Organization, form.Username)
password := form.Password
msg := object.CheckUserLogin(userId, password)
if msg != "" {
resp = Response{Status: "error", Msg: msg, Data: ""}
} else {
c.SetSessionUser(user)
c.SetSessionUser(userId)
util.LogInfo(c.Ctx, "API: [%s] logged in", user)
resp = Response{Status: "ok", Msg: "", Data: user}
util.LogInfo(c.Ctx, "API: [%s] logged in", userId)
resp = Response{Status: "ok", Msg: "", Data: userId}
}
c.Data["json"] = resp

View File

@ -20,6 +20,11 @@ import (
"github.com/casdoor/casdoor/object"
)
func (c *ApiController) GetGlobalUsers() {
c.Data["json"] = object.GetGlobalUsers()
c.ServeJSON()
}
func (c *ApiController) GetUsers() {
owner := c.Input().Get("owner")

View File

@ -31,6 +31,16 @@ type User struct {
Phone string `xorm:"varchar(100)" json:"phone"`
}
func GetGlobalUsers() []*User {
users := []*User{}
err := adapter.engine.Desc("created_time").Find(&users)
if err != nil {
panic(err)
}
return users
}
func GetUsers(owner string) []*User {
users := []*User{}
err := adapter.engine.Desc("created_time").Find(&users, &User{Owner: owner})

View File

@ -44,6 +44,7 @@ func initAPI() {
beego.Router("/api/add-organization", &controllers.ApiController{}, "POST:AddOrganization")
beego.Router("/api/delete-organization", &controllers.ApiController{}, "POST:DeleteOrganization")
beego.Router("/api/get-global-users", &controllers.ApiController{}, "GET:GetGlobalUsers")
beego.Router("/api/get-users", &controllers.ApiController{}, "GET:GetUsers")
beego.Router("/api/get-user", &controllers.ApiController{}, "GET:GetUser")
beego.Router("/api/update-user", &controllers.ApiController{}, "POST:UpdateUser")

View File

@ -66,7 +66,7 @@ class App extends Component {
}
}
onLogined() {
onLoggedIn() {
this.getAccount();
}
@ -226,7 +226,7 @@ class App extends Component {
}
}
renderLoginIfNotLogined(component) {
renderLoginIfNotLoggedIn(component) {
if (this.state.account === null) {
return <Redirect to='/login' />
} else if (this.state.account === undefined) {
@ -265,14 +265,14 @@ class App extends Component {
</Menu>
</Header>
<Switch>
<Route exact path="/organizations" component={OrganizationListPage}/>
<Route exact path="/organizations/:organizationName" component={OrganizationEditPage}/>
<Route exact path="/users" component={UserListPage}/>
<Route exact path="/users/:userName" component={UserEditPage}/>
<Route exact path="/providers" component={ProviderListPage}/>
<Route exact path="/providers/:providerName" component={ProviderEditPage}/>
<Route exact path="/applications" component={ApplicationListPage}/>
<Route exact path="/applications/:applicationName" component={ApplicationEditPage}/>
<Route exact path="/organizations" render={(props) => this.renderLoginIfNotLoggedIn(<OrganizationListPage account={this.state.account} {...props} />)}/>
<Route exact path="/organizations/:organizationName" render={(props) => this.renderLoginIfNotLoggedIn(<OrganizationEditPage account={this.state.account} {...props} />)}/>
<Route exact path="/users" render={(props) => this.renderLoginIfNotLoggedIn(<UserListPage account={this.state.account} {...props} />)}/>
<Route exact path="/users/:organizationName/:userName" render={(props) => this.renderLoginIfNotLoggedIn(<UserEditPage account={this.state.account} {...props} />)}/>
<Route exact path="/providers" render={(props) => this.renderLoginIfNotLoggedIn(<ProviderListPage account={this.state.account} {...props} />)}/>
<Route exact path="/providers/:providerName" render={(props) => this.renderLoginIfNotLoggedIn(<ProviderEditPage account={this.state.account} {...props} />)}/>
<Route exact path="/applications" render={(props) => this.renderLoginIfNotLoggedIn(<ApplicationListPage account={this.state.account} {...props} />)}/>
<Route exact path="/applications/:applicationName" render={(props) => this.renderLoginIfNotLoggedIn(<ApplicationEditPage account={this.state.account} {...props} />)}/>
</Switch>
</div>
)

View File

@ -40,7 +40,7 @@ class Face extends React.Component {
AccountBackend.login(values)
.then((res) => {
if (res.status === 'ok') {
this.props.onLogined();
this.props.onLoggedIn();
Setting.showMessage("success", `Logged in successfully`);
Setting.goToLink("/");
} else {
@ -53,7 +53,10 @@ class Face extends React.Component {
return (
<Form
name="normal_login"
initialValues={{ remember: true }}
initialValues={{
organization: this.getApplicationObj().organization,
remember: true
}}
onFinish={this.onFinish.bind(this)}
style={{width: "250px"}}
size="large"
@ -104,12 +107,15 @@ class Face extends React.Component {
render() {
const application = this.getApplicationObj();
if (application === null) {
return null;
}
return (
<Row>
<Col span={24} style={{display: "flex", justifyContent: "center"}} >
<div style={{marginTop: "80px", textAlign: "center"}}>
<img src={application?.logo} alt={application?.displayName} style={{marginBottom: '50px'}}/>
<img src={application.logo} alt={application.displayName} style={{marginBottom: '50px'}}/>
{
this.renderForm(application)
}

View File

@ -1,25 +1,30 @@
import React from "react";
import {Button, Card, Col, Input, Row} from 'antd';
import {LinkOutlined} from "@ant-design/icons";
import {Button, Card, Col, Input, Row, Select} from 'antd';
import * as UserBackend from "./backend/UserBackend";
import * as OrganizationBackend from "./backend/OrganizationBackend";
import * as Setting from "./Setting";
const { Option } = Select;
class UserEditPage extends React.Component {
constructor(props) {
super(props);
this.state = {
classes: props,
organizationName: props.match.params.organizationName,
userName: props.match.params.userName,
user: null,
organizations: [],
};
}
componentWillMount() {
this.getUser();
this.getOrganizations();
}
getUser() {
UserBackend.getUser("admin", this.state.userName)
UserBackend.getUser(this.state.organizationName, this.state.userName)
.then((user) => {
this.setState({
user: user,
@ -27,6 +32,15 @@ class UserEditPage extends React.Component {
});
}
getOrganizations() {
OrganizationBackend.getOrganizations("admin")
.then((res) => {
this.setState({
organizations: res,
});
});
}
parseUserField(key, value) {
// if ([].includes(key)) {
// value = Setting.myParseInt(value);
@ -53,6 +67,18 @@ class UserEditPage extends React.Component {
</div>
} style={{marginLeft: '5px'}} type="inner">
<Row style={{marginTop: '10px'}} >
<Col style={{marginTop: '5px'}} span={2}>
Organization:
</Col>
<Col span={22} >
<Select virtual={false} style={{width: '100%'}} value={this.state.user.owner} onChange={(value => {this.updateUserField('owner', value);})}>
{
this.state.organizations.map((organization, index) => <Option key={index} value={organization.name}>{organization.name}</Option>)
}
</Select>
</Col>
</Row>
<Row style={{marginTop: '20px'}} >
<Col style={{marginTop: '5px'}} span={2}>
Name:
</Col>
@ -118,16 +144,18 @@ class UserEditPage extends React.Component {
submitUserEdit() {
let user = Setting.deepCopy(this.state.user);
UserBackend.updateUser(this.state.user.owner, this.state.userName, user)
UserBackend.updateUser(this.state.organizationName, this.state.userName, user)
.then((res) => {
if (res) {
Setting.showMessage("success", `Successfully saved`);
this.setState({
organizationName: this.state.user.owner,
userName: this.state.user.name,
});
this.props.history.push(`/users/${this.state.user.name}`);
this.props.history.push(`/users/${this.state.user.owner}/${this.state.user.name}`);
} else {
Setting.showMessage("error", `failed to save: server side failure`);
this.updateUserField('owner', this.state.organizationName);
this.updateUserField('name', this.state.userName);
}
})

View File

@ -19,7 +19,7 @@ class UserListPage extends React.Component {
}
getUsers() {
UserBackend.getUsers("admin")
UserBackend.getGlobalUsers()
.then((res) => {
this.setState({
users: res,
@ -71,6 +71,20 @@ class UserListPage extends React.Component {
renderTable(users) {
const columns = [
{
title: 'Organization',
dataIndex: 'owner',
key: 'owner',
width: '120px',
sorter: (a, b) => a.owner.localeCompare(b.owner),
render: (text, record, index) => {
return (
<a href={`/organizations/${text}`}>
{text}
</a>
)
}
},
{
title: 'Name',
dataIndex: 'name',
@ -79,7 +93,7 @@ class UserListPage extends React.Component {
sorter: (a, b) => a.name.localeCompare(b.name),
render: (text, record, index) => {
return (
<Link to={`/users/${text}`}>
<Link to={`/users/${record.owner}/${text}`}>
{text}
</Link>
)
@ -138,7 +152,7 @@ class UserListPage extends React.Component {
render: (text, record, index) => {
return (
<div>
<Button style={{marginTop: '10px', marginBottom: '10px', marginRight: '10px'}} type="primary" onClick={() => this.props.history.push(`/users/${record.name}`)}>Edit</Button>
<Button style={{marginTop: '10px', marginBottom: '10px', marginRight: '10px'}} type="primary" onClick={() => this.props.history.push(`/users/${record.owner}/${record.name}`)}>Edit</Button>
<Popconfirm
title={`Sure to delete user: ${record.name} ?`}
onConfirm={() => this.deleteUser(index)}

View File

@ -1,5 +1,12 @@
import * as Setting from "../Setting";
export function getGlobalUsers() {
return fetch(`${Setting.ServerUrl}/api/get-global-users`, {
method: "GET",
credentials: "include"
}).then(res => res.json());
}
export function getUsers(owner) {
return fetch(`${Setting.ServerUrl}/api/get-users?owner=${owner}`, {
method: "GET",