forked from casdoor/casdoor
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: destroy session after delete user (casdoor#1441)
* fix: destroy session after delete user * feat: visual session * fix: go lint * feat: add translation * feat: auto flush after offline * fix: delete one session * fix: move 403 page to baseListPage
- Loading branch information
1 parent
4ab2ca7
commit b525210
Showing
39 changed files
with
12,349 additions
and
11,686 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// Copyright 2022 The Casdoor Authors. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package controllers | ||
|
||
import ( | ||
"encoding/json" | ||
|
||
"github.com/beego/beego/utils/pagination" | ||
"github.com/casdoor/casdoor/object" | ||
"github.com/casdoor/casdoor/util" | ||
) | ||
|
||
// DeleteSession | ||
// @Title DeleteSession | ||
// @Tag Session API | ||
// @Description Delete session by userId | ||
// @Param ID query string true "The ID(owner/name) of user." | ||
// @Success 200 {array} string The Response object | ||
// @router /delete-session [post] | ||
func (c *ApiController) DeleteSession() { | ||
var session object.Session | ||
err := json.Unmarshal(c.Ctx.Input.RequestBody, &session) | ||
if err != nil { | ||
c.ResponseError(err.Error()) | ||
return | ||
} | ||
|
||
c.Data["json"] = wrapActionResponse(object.DeleteSession(util.GetId(session.Owner, session.Name))) | ||
c.ServeJSON() | ||
} | ||
|
||
// GetSessions | ||
// @Title GetSessions | ||
// @Tag Session API | ||
// @Description Get organization user sessions | ||
// @Param owner query string true "The organization name" | ||
// @Success 200 {array} string The Response object | ||
// @router /get-sessions [get] | ||
func (c *ApiController) GetSessions() { | ||
limit := c.Input().Get("pageSize") | ||
page := c.Input().Get("p") | ||
field := c.Input().Get("field") | ||
value := c.Input().Get("value") | ||
sortField := c.Input().Get("sortField") | ||
sortOrder := c.Input().Get("sortOrder") | ||
owner := c.Input().Get("owner") | ||
if limit == "" || page == "" { | ||
c.Data["json"] = object.GetSessions(owner) | ||
c.ServeJSON() | ||
} else { | ||
limit := util.ParseInt(limit) | ||
paginator := pagination.SetPaginator(c.Ctx, limit, int64(object.GetSessionCount(owner, field, value))) | ||
sessions := object.GetPaginationSessions(owner, paginator.Offset(), limit, field, value, sortField, sortOrder) | ||
c.ResponseOk(sessions, paginator.Nums()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
// Copyright 2022 The Casdoor Authors. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package object | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/beego/beego" | ||
"github.com/casdoor/casdoor/util" | ||
"xorm.io/core" | ||
) | ||
|
||
type Session struct { | ||
Owner string `xorm:"varchar(100) notnull pk" json:"owner"` | ||
Name string `xorm:"varchar(100) notnull pk" json:"name"` | ||
CreatedTime string `xorm:"varchar(100)" json:"createdTime"` | ||
|
||
SessionId []string `json:"sessionId"` | ||
} | ||
|
||
func SetSession(id string, sessionId string) { | ||
owner, name := util.GetOwnerAndNameFromIdNoCheck(id) | ||
session := &Session{Owner: owner, Name: name} | ||
get, err := adapter.Engine.Get(session) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
session.SessionId = append(session.SessionId, sessionId) | ||
if get { | ||
_, err = adapter.Engine.ID(core.PK{owner, name}).Update(session) | ||
} else { | ||
session.CreatedTime = time.Now().Format(time.RFC3339) | ||
_, err = adapter.Engine.Insert(session) | ||
} | ||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
func DeleteSession(id string) bool { | ||
owner, name := util.GetOwnerAndNameFromIdNoCheck(id) | ||
|
||
session := &Session{Owner: owner, Name: name} | ||
_, err := adapter.Engine.ID(core.PK{owner, name}).Get(session) | ||
if err != nil { | ||
return false | ||
} | ||
|
||
DeleteBeegoSession(session.SessionId) | ||
|
||
affected, err := adapter.Engine.ID(core.PK{owner, name}).Delete(session) | ||
return affected != 0 | ||
} | ||
|
||
func DeleteSessionId(id string, sessionId string) bool { | ||
owner, name := util.GetOwnerAndNameFromIdNoCheck(id) | ||
|
||
session := &Session{Owner: owner, Name: name} | ||
_, err := adapter.Engine.ID(core.PK{owner, name}).Get(session) | ||
if err != nil { | ||
return false | ||
} | ||
|
||
DeleteBeegoSession([]string{sessionId}) | ||
session.SessionId = util.DeleteVal(session.SessionId, sessionId) | ||
|
||
if len(session.SessionId) < 1 { | ||
affected, _ := adapter.Engine.ID(core.PK{owner, name}).Delete(session) | ||
return affected != 0 | ||
} else { | ||
affected, _ := adapter.Engine.ID(core.PK{owner, name}).Update(session) | ||
return affected != 0 | ||
} | ||
} | ||
|
||
func DeleteBeegoSession(sessionIds []string) { | ||
for _, sessionId := range sessionIds { | ||
err := beego.GlobalSessions.GetProvider().SessionDestroy(sessionId) | ||
if err != nil { | ||
return | ||
} | ||
} | ||
} | ||
|
||
func GetSessions(owner string) []*Session { | ||
sessions := []*Session{} | ||
var err error | ||
if owner != "" { | ||
err = adapter.Engine.Desc("created_time").Where("owner = ?", owner).Find(&sessions) | ||
} else { | ||
err = adapter.Engine.Desc("created_time").Find(&sessions) | ||
} | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
return sessions | ||
} | ||
|
||
func GetPaginationSessions(owner string, offset, limit int, field, value, sortField, sortOrder string) []*Session { | ||
sessions := []*Session{} | ||
session := GetSession(owner, offset, limit, field, value, sortField, sortOrder) | ||
err := session.Find(&sessions) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
return sessions | ||
} | ||
|
||
func GetSessionCount(owner, field, value string) int { | ||
session := GetSession(owner, -1, -1, field, value, "", "") | ||
count, err := session.Count(&Session{}) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
return int(count) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Copyright 2022 The Casdoor Authors. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package util | ||
|
||
func DeleteVal(values []string, val string) []string { | ||
newValues := []string{} | ||
for _, v := range values { | ||
if v != val { | ||
newValues = append(newValues, v) | ||
} | ||
} | ||
return newValues | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.