Ejemplo n.º 1
0
func (l *LoginController) SavePassword(r *knot.WebContext) interface{} {
	r.Config.OutputType = knot.OutputJson

	payload := toolkit.M{}
	err := r.GetPayload(&payload)
	if err != nil {
		return helper.CreateResult(false, nil, err.Error())
	}

	if !payload.Has("newpassword") || !payload.Has("userid") {
		return helper.CreateResult(false, nil, "Data is not complete")
	}

	switch {
	case payload.Has("tokenid"):
		err = acl.ChangePasswordToken(toolkit.ToString(payload["userid"]), toolkit.ToString(payload["newpassword"]), toolkit.ToString(payload["tokenid"]))
	default:
		// check sessionid first
		savedsessionid := "" //change with get session
		//=======================
		userid, err := acl.FindUserBySessionID(savedsessionid)
		if err == nil && userid == toolkit.ToString(payload["userid"]) {
			err = acl.ChangePassword(toolkit.ToString(payload["userid"]), toolkit.ToString(payload["newpassword"]))
		} else if err == nil {
			err = errors.New("Userid is not match")
		}
	}

	return helper.CreateResult(true, nil, "save password success")
}
Ejemplo n.º 2
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
}
Ejemplo n.º 3
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), "")
}
Ejemplo n.º 4
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")
}