Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use Casbin RBAC model at server side #65

Merged
merged 1 commit into from
Aug 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
55 changes: 55 additions & 0 deletions authz/check.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright 2020 The casbin 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 authz

import (
"github.com/astaxie/beego"
"github.com/casbin/casbin/v2"
xormadapter "github.com/casbin/xorm-adapter/v2"
)

var Enforcer *casbin.Enforcer

func Init() {
var err error

a, err := xormadapter.NewAdapter("mysql", beego.AppConfig.String("dataSourceName")+beego.AppConfig.String("databaseName"), true)
if err != nil {
panic(err)
}

Enforcer, err = casbin.NewEnforcer("conf/rbac_model.conf", a)
if err != nil {
panic(err)
}

Enforcer.LoadPolicy()
}

func IsRootMod(memberId string) bool {
ret, err := Enforcer.HasRoleForUser(User(memberId), Role("root_mod"))
if err != nil {
panic(err)
}
return ret
}

func IsNodeMod(memberId string, nodeId string) bool {
ret, err := Enforcer.HasRoleForUser(User(memberId), Role("mod"+nodeId))
if err != nil {
panic(err)
}
return ret
}
45 changes: 45 additions & 0 deletions authz/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright 2020 The casbin 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 authz

import "strings"


func checkAndAddPrefix(s string, prefix string) string {
if strings.HasPrefix(s, prefix) {
return s
}
return prefix + s
}

func Role(role string) string {
return checkAndAddPrefix(role, "role::")
}

func User(user string) string {
return checkAndAddPrefix(user, "user::")
}

func Action(act string) string {
return checkAndAddPrefix(act, "act::")
}

func Object(obj string) string {
return checkAndAddPrefix(obj, "obj::")
}

func Node(obj string) string {
return checkAndAddPrefix(obj, "node::")
}
14 changes: 14 additions & 0 deletions conf/rbac_model.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[request_definition]
r = sub, obj, act

[policy_definition]
p = sub, obj, act

[role_definition]
g = _, _

[policy_effect]
e = some(where (p.eft == allow))

[matchers]
m = g(r.sub, p.sub) && r.obj == p.obj && r.act == p.act
5 changes: 3 additions & 2 deletions controllers/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package controllers
import (
"encoding/json"

. "github.com/casbin/casbin-forum/authz"
"github.com/casbin/casbin-forum/object"
"github.com/casbin/casbin-forum/util"
)
Expand Down Expand Up @@ -173,7 +174,7 @@ func (c *APIController) AddNodeModerators() {
var resp Response

memberId := c.GetSessionUser()
if !object.CheckModIdentity(memberId) {
if !IsRootMod(memberId) {
resp = Response{Status: "fail", Msg: "Unauthorized."}
c.Data["json"] = resp
c.ServeJSON()
Expand Down Expand Up @@ -209,7 +210,7 @@ func (c *APIController) DeleteNodeModerators() {
var resp Response

memberId := c.GetSessionUser()
if !object.CheckModIdentity(memberId) {
if !IsRootMod(memberId) {
resp = Response{Status: "fail", Msg: "Unauthorized."}
c.Data["json"] = resp
c.ServeJSON()
Expand Down
21 changes: 14 additions & 7 deletions controllers/topic.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ import (
"encoding/json"
"strconv"

"github.com/casbin/casbin-forum/authz"
. "github.com/casbin/casbin-forum/authz"

"github.com/casbin/casbin-forum/object"
"github.com/casbin/casbin-forum/util"
)
Expand Down Expand Up @@ -56,7 +59,7 @@ func (c *APIController) GetTopic() {

topic := object.GetTopicWithAvatar(id, memberId)
if memberId != "" {
topic.NodeModerator = object.CheckNodeModerator(memberId, topic.NodeId)
topic.NodeModerator = IsNodeMod(memberId, topic.NodeId)
}
if topic.Deleted {
c.Data["json"] = nil
Expand Down Expand Up @@ -146,7 +149,8 @@ func (c *APIController) DeleteTopic() {

id := util.ParseInt(idStr)
nodeId := object.GetTopicNodeId(id)
if !object.CheckModIdentity(memberId) && !object.CheckNodeModerator(memberId, nodeId) {

if !(IsNodeMod(memberId, nodeId) || IsRootMod(memberId)) {
resp := Response{Status: "fail", Msg: "Unauthorized."}
c.Data["json"] = resp
c.ServeJSON()
Expand Down Expand Up @@ -322,7 +326,8 @@ func (c *APIController) UpdateTopicNode() {
id, nodeName, nodeId := form.Id, form.NodeName, form.NodeId

originalNode := object.GetTopicNodeId(id)
if !object.CheckModIdentity(memberId) && !object.CheckNodeModerator(memberId, originalNode) && object.GetTopicAuthor(id) != memberId {
// TODO: Use Casbin ABAC to avoid "object.GetTopicAuthor(id) != memberId"
if !IsNodeMod(memberId, originalNode) && !IsRootMod(memberId) && object.GetTopicAuthor(id) != memberId {
resp = Response{Status: "fail", Msg: "Unauthorized."}
c.Data["json"] = resp
c.ServeJSON()
Expand Down Expand Up @@ -356,7 +361,8 @@ func (c *APIController) EditContent() {
panic(err)
}
id, title, content, nodeId := form.Id, form.Title, form.Content, form.NodeId
if !object.CheckModIdentity(memberId) && !object.CheckNodeModerator(memberId, nodeId) && object.GetTopicAuthor(id) != memberId {
// TODO: Use Casbin ABAC to avoid "object.GetTopicAuthor(id) != memberId"
if !IsRootMod(memberId) && authz.IsNodeMod(memberId, nodeId) && object.GetTopicAuthor(id) != memberId {
resp = Response{Status: "fail", Msg: "Unauthorized."}
c.Data["json"] = resp
c.ServeJSON()
Expand All @@ -378,7 +384,8 @@ func (c *APIController) EditContent() {
panic(err)
}
id, content := form.Id, form.Content
if !object.CheckModIdentity(memberId) && object.GetReplyAuthor(id) != memberId {
// TODO: Use Casbin ABAC to avoid "object.GetReplyAuthor(id) != memberId"
if !IsRootMod(memberId) && object.GetReplyAuthor(id) != memberId {
resp = Response{Status: "fail", Msg: "Unauthorized."}
c.Data["json"] = resp
c.ServeJSON()
Expand Down Expand Up @@ -412,7 +419,7 @@ func (c *APIController) TopTopic() {
var res bool

nodeId := object.GetTopicNodeId(id)
if object.CheckModIdentity(memberId) || object.CheckNodeModerator(memberId, nodeId) {
if IsRootMod(memberId) || IsNodeMod(memberId, nodeId) {
//timeStr := c.Input().Get("time")
//time := util.ParseInt(timeStr)
//date := util.GetTimeMinute(time)
Expand Down Expand Up @@ -457,7 +464,7 @@ func (c *APIController) CancelTopTopic() {
var res bool

nodeId := object.GetTopicNodeId(id)
if object.CheckModIdentity(memberId) || object.CheckNodeModerator(memberId, nodeId) {
if IsRootMod(memberId) || IsNodeMod(memberId, nodeId) {
topType := c.Input().Get("topType")
res = object.ChangeTopicTopExpiredTime(id, "", topType)
} else {
Expand Down
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@ require (
github.com/aliyun/aliyun-oss-go-sdk v2.1.4+incompatible
github.com/aliyun/aliyun-sts-go-sdk v0.0.0-20171106034748-98d3903a2309
github.com/astaxie/beego v1.12.1
github.com/casbin/casbin/v2 v2.10.0
github.com/casbin/xorm-adapter/v2 v2.0.1
github.com/dchest/captcha v0.0.0-20170622155422-6a29415a8364
github.com/go-gomail/gomail v0.0.0-20160411212932-81ebce5c23df
github.com/go-sql-driver/mysql v1.5.0
github.com/go-xorm/xorm v0.7.9 // indirect
github.com/mileusna/crontab v0.0.0-20180909013013-f182897de5d8
github.com/satori/go.uuid v1.2.0 // indirect
github.com/shiena/ansicolor v0.0.0-20151119151921-a422bbe96644 // indirect
golang.org/x/net v0.0.0-20190620200207-3b0461eec859
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect
xorm.io/xorm v0.8.1
)
23 changes: 22 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ cloud.google.com/go v0.37.4 h1:glPeL3BQJsbF6aIIYfZizMwc5LTYz250bDMjttbBGAU=
cloud.google.com/go v0.37.4/go.mod h1:NHPJ89PdicEuT9hdPXMROBD91xc5uRDxsMtSB16k7hw=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/Knetic/govaluate v3.0.0+incompatible/go.mod h1:r7JcOSlj0wfOMncg0iLm8Leh48TZaKVeNIfJntJ2wa0=
github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible h1:1G1pk05UrOh0NlF1oeaaix1x8XzrfjIDK47TY0Zehcw=
github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible/go.mod h1:r7JcOSlj0wfOMncg0iLm8Leh48TZaKVeNIfJntJ2wa0=
github.com/OwnLocal/goes v1.0.0/go.mod h1:8rIFjBGTue3lCU0wplczcUgt9Gxgrkkrw7etMIcn8TM=
github.com/Shopify/sarama v1.19.0/go.mod h1:FVkBWblsNy7DGZRfXLU0O9RCGt5g3g3yEuWXgklEdEo=
github.com/Shopify/toxiproxy v2.1.4+incompatible/go.mod h1:OXgGpZ6Cli1/URJOF1DMxUHB2q5Ap20/P/eIdh4G0pI=
Expand All @@ -23,9 +25,14 @@ github.com/beego/goyaml2 v0.0.0-20130207012346-5545475820dd/go.mod h1:1b+Y/CofkY
github.com/beego/x2j v0.0.0-20131220205130-a0352aadc542/go.mod h1:kSeGC/p1AbBiEp5kat81+DSQrZenVBZXklMLaELspWU=
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
github.com/bradfitz/gomemcache v0.0.0-20180710155616-bc664df96737/go.mod h1:PmM6Mmwb0LSuEubjR8N7PtNe1KxZLtOUHtbeikc5h60=
github.com/casbin/casbin v1.7.0/go.mod h1:c67qKN6Oum3UF5Q1+BByfFxkwKvhwW57ITjqwtzR1KE=
github.com/casbin/casbin/v2 v2.0.1/go.mod h1:YcPU1XXisHhLzuxH9coDNf2FbKpjGlbCg3n9yuLkIJQ=
github.com/casbin/casbin/v2 v2.10.0 h1:hBGwn5dnOiA2agPwpedz0a3ke/OVnWksyoHm7dW/bWc=
github.com/casbin/casbin/v2 v2.10.0/go.mod h1:XXtYGrs/0zlOsJMeRteEdVi/FsB0ph7KgNfjoCoJUD8=
github.com/casbin/xorm-adapter/v2 v2.0.1 h1:58QFRfVCFn3vR0W+a9rZYT9iXlfDyDWhWwkVzXJI/gQ=
github.com/casbin/xorm-adapter/v2 v2.0.1/go.mod h1:EJLxlF9LOvSVL7iJLrvTR8BRvZrIOjTmwi/tQf0Npm8=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/cloudflare/golz4 v0.0.0-20150217214814-ef862a3cdc58/go.mod h1:EOBUe0h4xcZ5GoxqC5SDxFQ8gwyZPKQoEzownBlhI80=
github.com/cockroachdb/apd v1.1.0/go.mod h1:8Sl8LxpKi29FqWXR16WEFZRNSz3SoPzUzeMeY4+DwBQ=
github.com/couchbase/go-couchbase v0.0.0-20181122212707-3e9b6e1258bb/go.mod h1:TWI8EKQMs5u5jLKW/tsb9VwauIrMIxQG1r5fMsswK5U=
github.com/couchbase/gomemcached v0.0.0-20181122193126-5125a94a666c/go.mod h1:srVSlQLB8iXBVXHgnqemxUXqN6FCvClgCMPCsjBDR7c=
github.com/couchbase/goutils v0.0.0-20180530154633-e865a1461c8a/go.mod h1:BQwMFlJzDjFDG3DJUdU0KORxn88UlsOULuxLExMh3Hs=
Expand Down Expand Up @@ -55,6 +62,9 @@ github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LB
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
github.com/go-xorm/sqlfiddle v0.0.0-20180821085327-62ce714f951a h1:9wScpmSP5A3Bk8V3XHWUcJmYTh+ZnlHVyc+A4oZYS3Y=
github.com/go-xorm/sqlfiddle v0.0.0-20180821085327-62ce714f951a/go.mod h1:56xuuqnHyryaerycW3BfssRdxQstACi0Epw/yC5E2xM=
github.com/go-xorm/xorm v0.7.9 h1:LZze6n1UvRmM5gpL9/U9Gucwqo6aWlFVlfcHKH10qA0=
github.com/go-xorm/xorm v0.7.9/go.mod h1:XiVxrMMIhFkwSkh96BW7PACl7UhLtx2iJIHMdmjh5sQ=
github.com/gofrs/uuid v3.2.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
github.com/gogo/protobuf v1.2.0/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
github.com/goji/httpauth v0.0.0-20160601135302-2da839ab0f4d/go.mod h1:nnjvkQ9ptGaCkuDUx6wNykzzlUixGxvkme+H/lnzb+A=
Expand All @@ -76,6 +86,8 @@ github.com/gorilla/context v1.1.1/go.mod h1:kBGZzfjB9CEq2AlWe17Uuf7NDRt0dE0s8S51
github.com/gorilla/mux v1.6.2/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/jackc/fake v0.0.0-20150926172116-812a484cc733/go.mod h1:WrMFNQdiFJ80sQsxDoMokWK1W5TQtxBFNpzWTD84ibQ=
github.com/jackc/pgx v3.6.0+incompatible/go.mod h1:0ZGrqGqkRlliWnWB4zKnWtjbSWbGkVEFm4TeybAXq+I=
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af h1:pmfjZENx5imkbgOkpRUYLnmbU7UEFbjtDA2hxJ1ichM=
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k=
github.com/json-iterator/go v1.1.5 h1:gL2yXlmiIo4+t+y32d4WGwOjKGYcGOuyrg46vadswDE=
Expand All @@ -93,6 +105,8 @@ github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/lib/pq v1.0.0 h1:X5PMW56eZitiTeO7tKzZxFCSpbFZJtkMMooicw2us9A=
github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/lib/pq v1.2.0 h1:LXpIM/LZ5xGFhOpXAQUIMM1HdyqzVYM13zNdjCEEcA0=
github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/mattn/go-sqlite3 v1.10.0 h1:jbhqpg7tQe4SupckyijYiy0mJJ/pRyHvXf7JdWK860o=
github.com/mattn/go-sqlite3 v1.10.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc=
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
Expand All @@ -109,7 +123,9 @@ github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1Cpa
github.com/openzipkin/zipkin-go v0.1.6/go.mod h1:QgAqvLzwWbR/WpD4A3cGpPtJrZXNIiJc5AZX7/PBEpw=
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
github.com/pierrec/lz4 v2.0.5+incompatible/go.mod h1:pdkljMzZIN41W+lC3N2tnIh5sFi+IEE17M5jbnwPHcY=
github.com/pkg/errors v0.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw=
Expand All @@ -124,6 +140,7 @@ github.com/satori/go.uuid v1.2.0 h1:0uYX9dsZ2yD7q2RtLRtPSdGDWzjeM3TbMJP9utgA0ww=
github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0=
github.com/shiena/ansicolor v0.0.0-20151119151921-a422bbe96644 h1:X+yvsM2yrEktyI+b2qND5gpH8YhURn0k8OCaeRnkINo=
github.com/shiena/ansicolor v0.0.0-20151119151921-a422bbe96644/go.mod h1:nkxAfR/5quYxwPZhyDxgasBMnRtBZd0FCEpawpjMUFg=
github.com/shopspring/decimal v0.0.0-20180709203117-cd690d0c9e24/go.mod h1:M+9NzErvs504Cn4c5DxATwIqPbtswREoFCre64PpcG4=
github.com/siddontang/go v0.0.0-20180604090527-bdc77568d726/go.mod h1:3yhqj7WBBfRhbBlzyOC3gUxftwsU0u8gqevxwIHQpMw=
github.com/siddontang/ledisdb v0.0.0-20181029004158-becf5f38d373/go.mod h1:mF1DpOSOUiJRMR+FDqaqu3EBqrybQtrDDszLUZ6oxPg=
github.com/siddontang/rdb v0.0.0-20150307021120-fc89ed2e418d/go.mod h1:AMEsy7v5z92TR1JKMkLLoaOQk++LVnOKL3ScbJ8GNGA=
Expand Down Expand Up @@ -204,6 +221,8 @@ google.golang.org/genproto v0.0.0-20190404172233-64821d5d2107/go.mod h1:VzzqZJRn
google.golang.org/grpc v1.17.0/go.mod h1:6QZJwpn2B+Zp71q/5VxRsJ6NXXVCE5NRUHRo+f3cWCs=
google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c=
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc h1:2gGKlE2+asNV9m7xrywl36YYNnBG5ZQ0r/BOOxqPpmk=
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc/go.mod h1:m7x9LTH6d71AHyAX77c9yqWCCa3UKHcVEj9y7hAtKDk=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand All @@ -219,7 +238,9 @@ honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWh
honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
xorm.io/builder v0.3.6 h1:ha28mQ2M+TFx96Hxo+iq6tQgnkC9IZkM6D8w9sKHHF8=
xorm.io/builder v0.3.6/go.mod h1:LEFAPISnRzG+zxaxj2vPicRwz67BdhFreKg8yv8/TgU=
xorm.io/core v0.7.2-0.20190928055935-90aeac8d08eb/go.mod h1:jJfd0UAEzZ4t87nbQYtVjmqpIODugN6PD2D9E+dJvdM=
xorm.io/core v0.7.2 h1:mEO22A2Z7a3fPaZMk6gKL/jMD80iiyNwRrX5HOv3XLw=
xorm.io/core v0.7.2/go.mod h1:jJfd0UAEzZ4t87nbQYtVjmqpIODugN6PD2D9E+dJvdM=
xorm.io/xorm v0.8.0/go.mod h1:ZkJLEYLoVyg7amJK/5r779bHyzs2AU8f8VMiP6BM7uY=
xorm.io/xorm v0.8.1 h1:4f2KXuQxVdaX3RdI3Fw81NzMiSpZeyCZt8m3sEVeIkQ=
xorm.io/xorm v0.8.1/go.mod h1:ZkJLEYLoVyg7amJK/5r779bHyzs2AU8f8VMiP6BM7uY=
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (

"github.com/astaxie/beego"
"github.com/astaxie/beego/plugins/cors"
"github.com/casbin/casbin-forum/authz"
"github.com/casbin/casbin-forum/controllers"
"github.com/casbin/casbin-forum/object"
"github.com/casbin/casbin-forum/routers"
Expand Down Expand Up @@ -56,5 +57,6 @@ func main() {
//controllers.InitBeegoSession()
object.InitTimer()

authz.Init()
beego.Run("0.0.0.0:" + port)
}
4 changes: 3 additions & 1 deletion object/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

package object

import . "github.com/casbin/casbin-forum/authz"

type UploadFileRecord struct {
Id int `xorm:"int notnull pk autoincr" json:"id"`
FileName string `xorm:"varchar(100)" json:"fileName"`
Expand Down Expand Up @@ -87,7 +89,7 @@ func DeleteFileRecord(id int) bool {
}

func FileEditable(memberId, author string) bool {
if CheckModIdentity(memberId) {
if IsRootMod(memberId) {
return true
}

Expand Down
14 changes: 0 additions & 14 deletions object/member.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,20 +290,6 @@ func UpdateMemberCheckinDate(id, date string) bool {
return affected != 0
}

func CheckModIdentity(memberId string) bool {
member := Member{}
existed, err := adapter.engine.Where("id = ?", memberId).Cols("is_moderator").Get(&member)
if err != nil {
panic(err)
}

if existed {
return member.IsModerator
} else {
return false
}
}

func UpdateMemberPassword(id, password string) bool {
member := new(Member)
member.Password = password
Expand Down