Skip to content
This repository has been archived by the owner on Jun 26, 2021. It is now read-only.

Feat/kavach #2

Merged
merged 3 commits into from
Jun 6, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 52 additions & 2 deletions action/organization/create.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package organization

import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"os"
"strconv"

"github.com/factly/identity/model"
"github.com/factly/identity/util/render"
"github.com/factly/kavach-server/model"
"github.com/factly/kavach-server/util/render"
)

// create create organization
Expand All @@ -30,9 +33,56 @@ func create(w http.ResponseWriter, r *http.Request) {

err = model.DB.Model(&model.OrganizationUser{}).Create(&permission).Error

if err != nil {
return
}

result := orgWithRole{}
result.Organization = *organization
result.Permission = permission

/* creating role of admins */
reqRole := &model.Role{}
reqRole.ID = "roles:org:" + fmt.Sprint(organization.ID) + ":admin"
reqRole.Members = []string{fmt.Sprint(userID)}

buf := new(bytes.Buffer)
json.NewEncoder(buf).Encode(&reqRole)
req, err := http.NewRequest("PUT", os.Getenv("KETO_API")+"/engines/acp/ory/regex/roles", buf)

if err != nil {
return
}

client := &http.Client{}
_, err = client.Do(req)

if err != nil {
return
}

/* creating policy for admins */
reqPolicy := &model.Policy{}
reqPolicy.ID = "org:" + fmt.Sprint(organization.ID) + ":admins"
reqPolicy.Subjects = []string{reqRole.ID}
reqPolicy.Resources = []string{"resources:org:" + fmt.Sprint(organization.ID) + ":<.*>"}
reqPolicy.Actions = []string{"actions:org:" + fmt.Sprint(organization.ID) + ":<.*>"}
reqPolicy.Effect = "allow"

buf = new(bytes.Buffer)
json.NewEncoder(buf).Encode(&reqPolicy)
req, err = http.NewRequest("PUT", os.Getenv("KETO_API")+"/engines/acp/ory/regex/policies", buf)

if err != nil {
return
}

client = &http.Client{}
_, err = client.Do(req)

if err != nil {
return
}

render.JSON(w, http.StatusCreated, result)
}
4 changes: 2 additions & 2 deletions action/organization/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"net/http"
"strconv"

"github.com/factly/identity/model"
"github.com/factly/identity/util/render"
"github.com/factly/kavach-server/model"
"github.com/factly/kavach-server/util/render"
"github.com/go-chi/chi"
)

Expand Down
7 changes: 3 additions & 4 deletions action/organization/details.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package organization

import (
"fmt"
"net/http"
"strconv"

"github.com/factly/identity/model"
"github.com/factly/identity/util/render"
"github.com/factly/kavach-server/model"
"github.com/factly/kavach-server/util/render"
"github.com/go-chi/chi"
)

Expand All @@ -21,7 +20,7 @@ func details(w http.ResponseWriter, r *http.Request) {
var permission model.OrganizationUser

userID, _ := strconv.Atoi(r.Header.Get("X-User"))
fmt.Println(userID)

model.DB.Model(&model.OrganizationUser{}).Where(&model.OrganizationUser{
UserID: uint(userID),
OrganizationID: uint(id),
Expand Down
4 changes: 2 additions & 2 deletions action/organization/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"net/http"
"strconv"

"github.com/factly/identity/model"
"github.com/factly/identity/util/render"
"github.com/factly/kavach-server/model"
"github.com/factly/kavach-server/util/render"
)

// list return all organizations
Expand Down
4 changes: 2 additions & 2 deletions action/organization/route.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package organization

import (
"github.com/factly/identity/action/organization/user"
"github.com/factly/identity/model"
"github.com/factly/kavach-server/action/organization/user"
"github.com/factly/kavach-server/model"
"github.com/go-chi/chi"
)

Expand Down
4 changes: 2 additions & 2 deletions action/organization/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"net/http"
"strconv"

"github.com/factly/identity/model"
"github.com/factly/identity/util/render"
"github.com/factly/kavach-server/model"
"github.com/factly/kavach-server/util/render"
"github.com/go-chi/chi"
)

Expand Down
47 changes: 30 additions & 17 deletions action/organization/user/create.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package user

import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"os"
"strconv"

"github.com/factly/identity/model"
"github.com/factly/identity/util/render"
"github.com/factly/kavach-server/model"
"github.com/factly/kavach-server/util/render"
"github.com/go-chi/chi"
)

Expand All @@ -15,6 +18,10 @@ type invite struct {
Role string `json:"role"`
}

type role struct {
Members []string `json:"members"`
}

// create return all user in organization
func create(w http.ResponseWriter, r *http.Request) {
organizationID := chi.URLParam(r, "organization_id")
Expand All @@ -24,22 +31,7 @@ func create(w http.ResponseWriter, r *http.Request) {
return
}

// check the permission of host
host := &model.OrganizationUser{}
hostID, _ := strconv.Atoi(r.Header.Get("X-User"))

err = model.DB.Model(&model.OrganizationUser{}).Where(&model.OrganizationUser{
OrganizationID: uint(orgID),
UserID: uint(hostID),
Role: "owner",
}).First(host).Error

if err != nil {
return
}

// FindOrCreate invitee

req := invite{}
json.NewDecoder(r.Body).Decode(&req)

Expand All @@ -49,6 +41,27 @@ func create(w http.ResponseWriter, r *http.Request) {
Email: req.Email,
})

if req.Role == "owner" {
/* creating policy for admins */
reqRole := &model.Role{}
reqRole.Members = []string{fmt.Sprint(invitee.ID)}

buf := new(bytes.Buffer)
json.NewEncoder(buf).Encode(&reqRole)
req, err := http.NewRequest("PUT", os.Getenv("KETO_API")+"/engines/acp/ory/regex/roles/roles:org:"+fmt.Sprint(orgID)+":admin/members", buf)

if err != nil {
return
}

client := &http.Client{}
_, err = client.Do(req)

if err != nil {
return
}
}

// Add user into organization
result := &model.OrganizationUser{}

Expand Down
46 changes: 28 additions & 18 deletions action/organization/user/delete.go
Original file line number Diff line number Diff line change
@@ -1,49 +1,59 @@
package user

import (
"fmt"
"net/http"
"os"
"strconv"

"github.com/factly/identity/model"
"github.com/factly/identity/util/render"
"github.com/factly/kavach-server/model"
"github.com/factly/kavach-server/util/render"
"github.com/go-chi/chi"
)

// create return all user in organization
func delete(w http.ResponseWriter, r *http.Request) {
/* Check if record exist */
organizationID := chi.URLParam(r, "organization_id")
orgID, err := strconv.Atoi(organizationID)

if err != nil {
return
}

// check the permission of host
host := &model.OrganizationUser{}
hostID, _ := strconv.Atoi(r.Header.Get("X-User"))

err = model.DB.Model(&model.OrganizationUser{}).Where(&model.OrganizationUser{
OrganizationID: uint(orgID),
UserID: uint(hostID),
Role: "owner",
}).First(host).Error
permissionID := chi.URLParam(r, "permission_id")
pID, err := strconv.Atoi(permissionID)

if err != nil {
return
}

permissionID := chi.URLParam(r, "permission_id")
pID, errPermission := strconv.Atoi(permissionID)
result := &model.OrganizationUser{}
result.ID = uint(pID)

if errPermission != nil {
err = model.DB.First(&result).Error
if err != nil {
return
}

result := &model.OrganizationUser{}
result.ID = uint(pID)
result.OrganizationID = uint(orgID)
/* delete policy for admins */
if result.Role == "owner" {
req, err := http.NewRequest("DELETE", os.Getenv("KETO_API")+"/engines/acp/ory/regex/roles/roles:org:"+fmt.Sprint(orgID)+":admin/members/"+fmt.Sprint(result.UserID), nil)

if err != nil {
return
}

client := &http.Client{}
_, err = client.Do(req)

if err != nil {
return
}
}

model.DB.Model(&model.OrganizationUser{}).Delete(result)
/* DELETE */
model.DB.Delete(&result)

render.JSON(w, http.StatusOK, nil)
}
4 changes: 2 additions & 2 deletions action/organization/user/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"net/http"
"strconv"

"github.com/factly/identity/model"
"github.com/factly/identity/util/render"
"github.com/factly/kavach-server/model"
"github.com/factly/kavach-server/util/render"
"github.com/go-chi/chi"
)

Expand Down
4 changes: 2 additions & 2 deletions action/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package action
import (
"net/http"

"github.com/factly/identity/action/organization"
"github.com/factly/identity/action/user"
"github.com/factly/kavach-server/action/organization"
"github.com/factly/kavach-server/action/user"
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
"github.com/go-chi/cors"
Expand Down
4 changes: 2 additions & 2 deletions action/user/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"net/http"
"net/url"

"github.com/factly/identity/model"
"github.com/factly/identity/util/render"
"github.com/factly/kavach-server/model"
"github.com/factly/kavach-server/util/render"
)

type authenticationSession struct {
Expand Down
4 changes: 2 additions & 2 deletions action/user/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package user
import (
"net/http"

"github.com/factly/identity/model"
"github.com/factly/identity/util/render"
"github.com/factly/kavach-server/model"
"github.com/factly/kavach-server/util/render"
)

// list return all organizations
Expand Down
4 changes: 2 additions & 2 deletions action/user/me.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"net/http"
"strconv"

"github.com/factly/identity/model"
"github.com/factly/identity/util/render"
"github.com/factly/kavach-server/model"
"github.com/factly/kavach-server/util/render"
)

// create create organization
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/factly/identity
module github.com/factly/kavach-server

go 1.13

Expand Down
11 changes: 3 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
package main

import (
"log"
"net/http"
"os"

"github.com/factly/identity/model"
"github.com/factly/kavach-server/model"
"github.com/joho/godotenv"

"github.com/factly/identity/action"
"github.com/factly/kavach-server/action"
)

func main() {
err := godotenv.Load()

if err != nil {
log.Fatal("error loding .env file")
}
godotenv.Load()

port, ok := os.LookupEnv("PORT")
if !ok {
Expand Down
Loading