Example #1
0
func (a *SessionController) GetSession(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())
	// }

	tSession := new(acl.Session)

	data := toolkit.M{}

	arrm := make([]toolkit.M, 0, 0)
	// c, err := acl.Find(tSession, nil, toolkit.M{}.Set("take", payload["take"].(int)).Set("skip", payload["skip"].(int)))
	c, err := acl.Find(tSession, nil, toolkit.M{}.Set("take", 10).Set("skip", 0))
	if err != nil {
		return helper.CreateResult(true, nil, err.Error())
	}

	err = c.Fetch(&arrm, 0, false)
	for i, val := range arrm {
		tUser := new(acl.User)
		e := acl.FindByID(tUser, toolkit.ToString(val.Get("userid", "")))
		if e != nil {
			continue
		}
		arrm[i].Set("username", tUser.LoginID)
		arrm[i].Set("duration", time.Since(val["created"].(time.Time)).Hours())
		arrm[i].Set("status", "ACTIVE")
		if val["expired"].(time.Time).Before(time.Now().UTC()) {
			arrm[i].Set("duration", val["expired"].(time.Time).Sub(val["created"].(time.Time)).Hours())
			arrm[i].Set("status", "EXPIRED")
		}
		arrm[i].Set("username", tUser.LoginID)
		// toolkit.Printf("Debug date : %v : %v\n", toolkit.TypeName(val["created"]), val["created"].(time.Time))
	}
	c.Close()

	c, err = acl.Find(tSession, nil, nil)

	data.Set("Datas", arrm)
	data.Set("total", c.Count())
	// data.Set("total", 100)

	if err != nil {
		return helper.CreateResult(true, nil, err.Error())
	} else {
		return helper.CreateResult(true, data, "")
	}

}
Example #2
0
func (a *AdministrationController) FindAccess(r *knot.WebContext) interface{} {
	r.Config.OutputType = knot.OutputJson
	a.InitialSetDatabase()
	payload := map[string]interface{}{}
	err := r.GetPayload(&payload)
	tAccess := new(acl.Access)
	err = acl.FindByID(tAccess, payload["_id"].(string))
	if err != nil {
		return helper.CreateResult(true, nil, err.Error())
	} else {
		return helper.CreateResult(true, tAccess, "")
	}
}
Example #3
0
func (a *GroupController) FindGroup(r *knot.WebContext) interface{} {
	r.Config.OutputType = knot.OutputJson
	a.InitialSetDatabase()
	payload := map[string]interface{}{}
	err := r.GetPayload(&payload)
	tGroup := new(acl.Group)
	err = acl.FindByID(tGroup, payload["_id"].(string))
	if err != nil {
		return helper.CreateResult(true, nil, err.Error())
	} else {
		return helper.CreateResult(true, tGroup, "")
	}

}
Example #4
0
func (a *UserController) DeleteUser(r *knot.WebContext) interface{} {
	r.Config.OutputType = knot.OutputJson
	a.InitialSetDatabase()
	tUser := new(acl.User)
	payload := map[string]interface{}{}
	err := r.GetPayload(&payload)
	err = acl.FindByID(tUser, payload["_id"].(string))

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

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

	return helper.CreateResult(true, nil, "success")
}
Example #5
0
func GetUser(r *knot.WebContext) (tUser acl.User, err error) {
	sessionId := r.Session("sessionid", "")

	if toolkit.ToString(sessionId) == "" {
		err = error(errors.New("Sessionid is not found"))
		return
	}

	userid, err := acl.FindUserBySessionID(toolkit.ToString(sessionId))
	if err != nil {
		return
	}

	err = acl.FindByID(&tUser, userid)
	if err != nil {
		return
	}

	return
}
Example #6
0
func (a *UserController) GetAccess(r *knot.WebContext) interface{} {
	r.Config.OutputType = knot.OutputJson
	a.InitialSetDatabase()
	payload := map[string]interface{}{}
	err := r.GetPayload(&payload)
	tUser := new(acl.User)
	err = acl.FindByID(tUser, payload["id"].(string)) //
	if err != nil {
		return helper.CreateResult(true, nil, err.Error())
	}
	var AccessGrants = []interface{}{}
	for _, v := range tUser.Grants {
		var access = toolkit.M{}
		access.Set("AccessID", v.AccessID)
		access.Set("AccessValue", acl.Splitinttogrant(int(v.AccessValue)))
		AccessGrants = append(AccessGrants, access)
	}
	fmt.Println(AccessGrants)
	return helper.CreateResult(true, AccessGrants, "")
}
Example #7
0
func (l *LoginController) GetUserName(r *knot.WebContext) interface{} {

	r.Config.OutputType = knot.OutputJson
	sessionId := r.Session("sessionid", "")
	if toolkit.ToString(sessionId) == "" {
		return helper.CreateResult(true, "", "Sessionid is not found")
	}

	userid, err := acl.FindUserBySessionID(toolkit.ToString(sessionId))
	if err != nil {
		return helper.CreateResult(false, "", "Get username failed")
	}

	tUser := new(acl.User)
	err = acl.FindByID(tUser, userid)
	if err != nil {
		return helper.CreateResult(false, "", "Get username failed")
	}

	return helper.CreateResult(true, toolkit.M{}.Set("username", tUser.LoginID), "")
}
Example #8
0
func (a *SessionController) SetExpired(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())
	}

	tSession := new(acl.Session)
	err = acl.FindByID(tSession, payload["_id"].(string))
	if err != nil {
		return helper.CreateResult(false, nil, err.Error())
	}

	tSession.Expired = time.Now().UTC()
	err = acl.Save(tSession)
	if err != nil {
		return helper.CreateResult(false, nil, err.Error())
	}

	return helper.CreateResult(true, nil, "Set expired success")
}
Example #9
0
func (a *GroupController) RefreshGroupLdap(r *knot.WebContext) interface{} {
	r.Config.OutputType = knot.OutputJson
	payload := map[string]interface{}{}
	err := r.GetPayload(&payload)
	if err != nil {
		return helper.CreateResult(false, nil, err.Error())
	}

	group := new(acl.Group)
	err = acl.FindByID(group, payload["groupid"].(string))
	if err != nil {
		return helper.CreateResult(true, nil, err.Error())
	}
	config, err := toolkit.ToM(group.MemberConf)
	if err != nil {
		return helper.CreateResult(true, nil, err.Error())
	}
	err = acl.RefreshUserLdapByGroup(payload["groupid"].(string), config)
	if err != nil {
		return helper.CreateResult(true, nil, err.Error())
	}

	return helper.CreateResult(true, nil, "success")
}
Example #10
0
func (l *LoginController) GetAccessMenu(r *knot.WebContext) interface{} {
	r.Config.OutputType = knot.OutputJson
	sessionId := r.Session("sessionid", "")

	cursor, err := colonycore.Find(new(colonycore.Menu), nil)
	if err != nil {
		return helper.CreateResult(false, nil, err.Error())
	}
	defer cursor.Close()

	menus := []colonycore.Menu{}
	results := make([]toolkit.M, 0, 0)

	cursor.Fetch(&menus, 0, false)

	if IsDevMode {
		for _, m := range menus {
			result, _ := toolkit.ToM(m)
			results = append(results, result)
		}
		return helper.CreateResult(true, results, "Success")
	}

	if toolkit.ToString(sessionId) == "" {
		return helper.CreateResult(true, nil, "Session Not Found")
	}

	stat := acl.IsSessionIDActive(toolkit.ToString(sessionId))
	if !stat {
		return helper.CreateResult(false, nil, "Session Expired")
	}

	if cursor.Count() > 0 {
		for _, m := range menus {
			result := toolkit.M{}

			acc := acl.HasAccess(toolkit.ToString(sessionId), acl.IDTypeSession, m.AccessId, acl.AccessRead)
			result, err = toolkit.ToM(m)
			if err != nil {
				return helper.CreateResult(false, nil, err.Error())
			}

			// if toolkit.ToString(sessionId) != "" {
			userid, err := acl.FindUserBySessionID(toolkit.ToString(sessionId))
			if err != nil {
				return helper.CreateResult(false, "", "Get username failed")
			}
			tUser := new(acl.User)
			err = acl.FindByID(tUser, userid)
			if err != nil {
				return helper.CreateResult(false, "", "Get username failed")
			}

			result.Set("detail", 7)

			if tUser.LoginID == "eaciit" {
				results = append(results, result)
			} else {
				if acc {
					result.Set("childrens", "")
					if len(m.Childrens) > 0 {
						childs := GetChildMenu(r, m.Childrens)
						result.Set("childrens", childs)
					}
					results = append(results, result)
				}
			}
			// }
		}
	}

	return helper.CreateResult(true, results, "Success")
}