コード例 #1
0
ファイル: admin.go プロジェクト: robxu9/kahinah
// AdminPostHandler manipulates the central dashboard for kahinah
func AdminPostHandler(ctx context.Context, rw http.ResponseWriter, r *http.Request) {
	adminCheck(r)

	user := r.FormValue("username")
	action := r.FormValue("action")
	permission := r.FormValue("permission")

	if user == "" || (action != "add" && action != "rm") || permission == "" {
		panic(ErrBadRequest)
	}

	modelUser := models.FindUser(user)

	if action == "add" {
		if err := models.DB.Model(modelUser).Association("Permissions").Append(models.UserPermission{
			Permission: permission,
		}).Error; err != nil {
			panic(err)
		}
	} else {
		if err := models.DB.Model(modelUser).Association("Permissions").Delete(models.UserPermission{
			Permission: permission,
		}).Error; err != nil {
			panic(err)
		}
	}

	http.Redirect(rw, r, render.ConvertURL("/admin"), http.StatusTemporaryRedirect)
}
コード例 #2
0
ファイル: user.go プロジェクト: robxu9/kahinah
func UserLoginHandler(ctx context.Context, rw http.ResponseWriter, r *http.Request) {
	if !cas.IsAuthenticated(r) {
		// save the referrer
		sessionmw.Set(ctx, CASReferrer, r.Referer())

		// shut off rendering
		dataRenderer := data.FromContext(ctx)
		dataRenderer.Type = data.DataNoRender

		// and redirect
		cas.RedirectToLogin(rw, r)
	} else {
		// get the referrer
		referrer, has := sessionmw.Get(ctx, CASReferrer)
		sessionmw.Delete(ctx, CASReferrer)

		// shut off rendering
		dataRenderer := data.FromContext(ctx)
		dataRenderer.Type = data.DataNoRender

		// and redirect
		if !has {
			http.Redirect(rw, r, render.ConvertURL("/"), http.StatusTemporaryRedirect)
		} else {
			http.Redirect(rw, r, referrer.(string), http.StatusTemporaryRedirect)
		}
	}
}
コード例 #3
0
ファイル: activity.go プロジェクト: robxu9/kahinah
func ActivityJSONHandler(ctx context.Context, rw http.ResponseWriter, r *http.Request) {
	dataRenderer := data.FromContext(ctx)

	page := int(to.Int64(r.FormValue("page")))
	if page <= 0 {
		page = 1
	}

	limit := int(to.Int64(r.FormValue("limit")))
	if limit <= 0 {
		limit = 50
	}

	var cnt int
	if err := models.DB.Model(&models.ListActivity{}).Count(&cnt).Error; err != nil {
		panic(err)
	}

	totalpages := cnt / 50
	if cnt%50 != 0 {
		totalpages++
	}

	if page > totalpages {
		page = totalpages
	}

	var activities []models.ListActivity
	if err := models.DB.Limit(limit).Offset((page - 1) * limit).Order("created_at desc").Find(&activities).Error; err != nil && err != gorm.ErrRecordNotFound {
		panic(err)
	}

	// render a better karma view
	var rendered []*activityJSON
	for _, v := range activities {
		// load the username...
		rendered = append(rendered, &activityJSON{
			ListId:  v.ListID,
			User:    models.FindUserByID(v.UserID).Username,
			Comment: string(bluemonday.UGCPolicy().SanitizeBytes(blackfriday.MarkdownCommon([]byte(v.Activity)))),
			Time:    v.CreatedAt,
			URL:     render.ConvertURL("/b/" + to.String(v.ListID)),
		})
	}

	dataRenderer.Data = map[string]interface{}{
		"totalpages": totalpages,
		"page":       page,
		"activities": rendered,
	}
	dataRenderer.Type = data.DataJSON
}