Example #1
0
func (a *GroupController) SaveGroup(r *knot.WebContext) interface{} {
	r.Config.OutputType = knot.OutputJson
	a.InitialSetDatabase()
	payload := map[string]interface{}{}
	err := r.GetPayload(&payload)
	if err != nil {
		return helper.CreateResult(false, nil, err.Error())
	}
	g := payload["group"].(map[string]interface{})

	initGroup := new(acl.Group)
	initGroup.ID = g["_id"].(string)
	initGroup.Title = g["Title"].(string)
	initGroup.Owner = g["Owner"].(string)
	initGroup.Enable = g["Enable"].(bool)
	err = acl.Save(initGroup)
	if err != nil {
		return helper.CreateResult(true, nil, err.Error())
	}
	var grant map[string]interface{}
	for _, p := range payload["grants"].([]interface{}) {
		dat := []byte(p.(string))
		if err = json.Unmarshal(dat, &grant); err != nil {
			return helper.CreateResult(true, nil, err.Error())
		}
		AccessID := grant["AccessID"].(string)
		Accessvalue := grant["AccessValue"]
		for _, v := range Accessvalue.([]interface{}) {
			switch v {
			case "AccessCreate":
				initGroup.Grant(AccessID, acl.AccessCreate)
			case "AccessRead":
				initGroup.Grant(AccessID, acl.AccessRead)
			case "AccessUpdate":
				initGroup.Grant(AccessID, acl.AccessUpdate)
			case "AccessDelete":
				initGroup.Grant(AccessID, acl.AccessDelete)
			case "AccessSpecial1":
				initGroup.Grant(AccessID, acl.AccessSpecial1)
			case "AccessSpecial2":
				initGroup.Grant(AccessID, acl.AccessSpecial2)
			case "AccessSpecial3":
				initGroup.Grant(AccessID, acl.AccessSpecial3)
			case "AccessSpecial4":
				initGroup.Grant(AccessID, acl.AccessSpecial4)
			}
		}
	}
	err = acl.Save(initGroup)
	if err != nil {
		return helper.CreateResult(true, nil, err.Error())
	}
	return helper.CreateResult(true, nil, "sukses")
}
Example #2
0
func (a *GroupController) SaveGroup(r *knot.WebContext) interface{} {
	r.Config.OutputType = knot.OutputJson
	a.InitialSetDatabase()
	payload := map[string]interface{}{}
	err := r.GetPayload(&payload)
	if err != nil {
		return helper.CreateResult(false, nil, err.Error())
	}
	g := payload["group"].(map[string]interface{})
	config := payload["groupConfig"].(map[string]interface{})
	memberConf, _ := toolkit.ToM(config)

	if g["GroupType"].(string) == "1" {
		memberConf.Set("filter", "("+g["Filter"].(string)+")").
			Set("attributes", []string{g["LoginID"].(string), g["Fullname"].(string), g["Email"].(string)}).
			Set("mapattributes", toolkit.M{}.Set("LoginID", g["LoginID"].(string)).
				Set("FullName", g["Fullname"].(string)).
				Set("Email", g["Email"].(string)))

		err = acl.AddUserLdapByGroup(g["_id"].(string), memberConf)
		if err != nil {
			return helper.CreateResult(false, nil, err.Error())
		}
		delete(config, "password")
		delete(memberConf, "password")
	}

	initGroup := new(acl.Group)
	initGroup.ID = g["_id"].(string)
	initGroup.Title = g["Title"].(string)
	initGroup.Owner = g["Owner"].(string)
	initGroup.Enable = g["Enable"].(bool)
	initGroup.GroupConf = config
	initGroup.MemberConf = memberConf

	if g["GroupType"].(string) == "1" {
		initGroup.GroupType = acl.GroupTypeLdap
	} else if g["GroupType"].(string) == "0" {
		initGroup.GroupType = acl.GroupTypeBasic
	}

	err = acl.Save(initGroup)
	if err != nil {
		return helper.CreateResult(true, nil, err.Error())
	}

	var grant map[string]interface{}
	for _, p := range payload["grants"].([]interface{}) {
		dat := []byte(p.(string))
		if err = json.Unmarshal(dat, &grant); err != nil {
			return helper.CreateResult(true, nil, err.Error())
		}
		AccessID := grant["AccessID"].(string)
		Accessvalue := grant["AccessValue"]
		for _, v := range Accessvalue.([]interface{}) {
			switch v {
			case "AccessCreate":
				initGroup.Grant(AccessID, acl.AccessCreate)
			case "AccessRead":
				initGroup.Grant(AccessID, acl.AccessRead)
			case "AccessUpdate":
				initGroup.Grant(AccessID, acl.AccessUpdate)
			case "AccessDelete":
				initGroup.Grant(AccessID, acl.AccessDelete)
			case "AccessSpecial1":
				initGroup.Grant(AccessID, acl.AccessSpecial1)
			case "AccessSpecial2":
				initGroup.Grant(AccessID, acl.AccessSpecial2)
			case "AccessSpecial3":
				initGroup.Grant(AccessID, acl.AccessSpecial3)
			case "AccessSpecial4":
				initGroup.Grant(AccessID, acl.AccessSpecial4)
			}
		}
	}
	err = acl.Save(initGroup)
	if err != nil {
		return helper.CreateResult(true, nil, err.Error())
	}

	return helper.CreateResult(true, nil, "sukses")
}