2017-04-12 08:27:57 -04:00
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
2015-06-14 23:53:32 -08:00
// See License.txt for license information.
package api
import (
2015-09-16 09:37:20 -07:00
"fmt"
2015-06-14 23:53:32 -08:00
"net/http"
"net/url"
"strings"
2016-11-22 11:05:54 -08:00
"time"
2015-08-25 11:06:11 -07:00
2016-01-11 09:12:51 -06:00
l4g "github.com/alecthomas/log4go"
2016-04-21 22:37:01 -07:00
"github.com/gorilla/mux"
goi18n "github.com/nicksnyder/go-i18n/i18n"
2017-01-13 13:53:37 -05:00
"github.com/mattermost/platform/app"
2016-08-04 09:25:37 -08:00
"github.com/mattermost/platform/einterfaces"
2015-08-25 11:06:11 -07:00
"github.com/mattermost/platform/model"
"github.com/mattermost/platform/utils"
2015-06-14 23:53:32 -08:00
)
type Context struct {
2017-09-06 17:12:54 -05:00
App * app . App
2017-01-30 08:30:02 -05:00
Session model . Session
RequestId string
IpAddress string
Path string
Err * model . AppError
2017-04-04 11:54:52 -04:00
siteURLHeader string
2017-01-30 08:30:02 -05:00
teamURLValid bool
teamURL string
T goi18n . TranslateFunc
Locale string
TeamId string
isSystemAdmin bool
2015-06-14 23:53:32 -08:00
}
func ApiAppHandler ( h func ( * Context , http . ResponseWriter , * http . Request ) ) http . Handler {
2016-12-12 08:16:10 -05:00
return & handler { h , false , false , true , false , false , false , false }
2015-06-14 23:53:32 -08:00
}
func AppHandler ( h func ( * Context , http . ResponseWriter , * http . Request ) ) http . Handler {
2016-12-12 08:16:10 -05:00
return & handler { h , false , false , false , false , false , false , false }
2015-07-08 11:50:10 -04:00
}
func AppHandlerIndependent ( h func ( * Context , http . ResponseWriter , * http . Request ) ) http . Handler {
2016-12-12 08:16:10 -05:00
return & handler { h , false , false , false , false , true , false , false }
2015-06-14 23:53:32 -08:00
}
func ApiUserRequired ( h func ( * Context , http . ResponseWriter , * http . Request ) ) http . Handler {
2016-12-12 08:16:10 -05:00
return & handler { h , true , false , true , false , false , false , true }
2015-06-14 23:53:32 -08:00
}
func ApiUserRequiredActivity ( h func ( * Context , http . ResponseWriter , * http . Request ) , isUserActivity bool ) http . Handler {
2016-12-12 08:16:10 -05:00
return & handler { h , true , false , true , isUserActivity , false , false , true }
}
func ApiUserRequiredMfa ( h func ( * Context , http . ResponseWriter , * http . Request ) ) http . Handler {
return & handler { h , true , false , true , false , false , false , false }
2015-06-14 23:53:32 -08:00
}
func UserRequired ( h func ( * Context , http . ResponseWriter , * http . Request ) ) http . Handler {
2016-12-12 08:16:10 -05:00
return & handler { h , true , false , false , false , false , false , true }
2015-06-14 23:53:32 -08:00
}
2016-08-03 12:19:27 -05:00
func AppHandlerTrustRequester ( h func ( * Context , http . ResponseWriter , * http . Request ) ) http . Handler {
2016-12-12 08:16:10 -05:00
return & handler { h , false , false , false , false , false , true , false }
2016-08-03 12:19:27 -05:00
}
2015-06-14 23:53:32 -08:00
func ApiAdminSystemRequired ( h func ( * Context , http . ResponseWriter , * http . Request ) ) http . Handler {
2016-12-12 08:16:10 -05:00
return & handler { h , true , true , true , false , false , false , true }
2016-04-04 14:58:05 -04:00
}
2016-09-13 12:42:48 -04:00
func ApiAdminSystemRequiredTrustRequester ( h func ( * Context , http . ResponseWriter , * http . Request ) ) http . Handler {
2016-12-12 08:16:10 -05:00
return & handler { h , true , true , true , false , false , true , true }
2016-09-13 12:42:48 -04:00
}
2016-04-04 14:58:05 -04:00
func ApiAppHandlerTrustRequester ( h func ( * Context , http . ResponseWriter , * http . Request ) ) http . Handler {
2016-12-12 08:16:10 -05:00
return & handler { h , false , false , true , false , false , true , false }
2016-04-04 14:58:05 -04:00
}
func ApiUserRequiredTrustRequester ( h func ( * Context , http . ResponseWriter , * http . Request ) ) http . Handler {
2016-12-12 08:16:10 -05:00
return & handler { h , true , false , true , false , false , true , true }
2015-06-14 23:53:32 -08:00
}
2016-05-05 16:35:03 -04:00
func ApiAppHandlerTrustRequesterIndependent ( h func ( * Context , http . ResponseWriter , * http . Request ) ) http . Handler {
2016-12-12 08:16:10 -05:00
return & handler { h , false , false , true , false , true , true , false }
2016-05-05 16:35:03 -04:00
}
2015-06-14 23:53:32 -08:00
type handler struct {
handleFunc func ( * Context , http . ResponseWriter , * http . Request )
requireUser bool
requireSystemAdmin bool
isApi bool
isUserActivity bool
2015-07-08 11:50:10 -04:00
isTeamIndependent bool
2016-04-04 14:58:05 -04:00
trustRequester bool
2016-12-12 08:16:10 -05:00
requireMfa bool
2015-06-14 23:53:32 -08:00
}
func ( h handler ) ServeHTTP ( w http . ResponseWriter , r * http . Request ) {
2016-11-22 11:05:54 -08:00
now := time . Now ( )
2015-06-14 23:53:32 -08:00
l4g . Debug ( "%v" , r . URL . Path )
2017-03-14 14:56:35 -04:00
if metrics := einterfaces . GetMetricsInterface ( ) ; metrics != nil && h . isApi {
metrics . IncrementHttpRequest ( )
}
2015-06-14 23:53:32 -08:00
c := & Context { }
2017-09-06 17:12:54 -05:00
c . App = app . Global ( )
2016-01-21 14:15:44 -06:00
c . T , c . Locale = utils . GetTranslationsAndLocale ( w , r )
2015-06-14 23:53:32 -08:00
c . RequestId = model . NewId ( )
2017-01-13 13:53:37 -05:00
c . IpAddress = utils . GetIpAddress ( r )
2016-04-21 22:37:01 -07:00
c . TeamId = mux . Vars ( r ) [ "team_id" ]
2015-06-14 23:53:32 -08:00
2015-09-16 15:49:12 -04:00
token := ""
isTokenFromQueryString := false
// Attempt to parse token out of the header
authHeader := r . Header . Get ( model . HEADER_AUTH )
if len ( authHeader ) > 6 && strings . ToUpper ( authHeader [ 0 : 6 ] ) == model . HEADER_BEARER {
// Default session token
token = authHeader [ 7 : ]
} else if len ( authHeader ) > 5 && strings . ToLower ( authHeader [ 0 : 5 ] ) == model . HEADER_TOKEN {
// OAuth token
token = authHeader [ 6 : ]
}
// Attempt to parse the token from the cookie
if len ( token ) == 0 {
2016-02-08 07:26:10 -05:00
if cookie , err := r . Cookie ( model . SESSION_COOKIE_TOKEN ) ; err == nil {
token = cookie . Value
2016-04-04 14:58:05 -04:00
if ( h . requireSystemAdmin || h . requireUser ) && ! h . trustRequester {
if r . Header . Get ( model . HEADER_REQUESTED_WITH ) != model . HEADER_REQUESTED_WITH_XML {
2017-09-01 14:58:43 +01:00
c . Err = model . NewAppError ( "ServeHTTP" , "api.context.session_expired.app_error" , nil , "token=" + token + " Appears to be a CSRF attempt" , http . StatusUnauthorized )
2016-04-04 14:58:05 -04:00
token = ""
}
}
2015-09-16 15:49:12 -04:00
}
}
// Attempt to parse token out of the query string
if len ( token ) == 0 {
token = r . URL . Query ( ) . Get ( "access_token" )
isTokenFromQueryString = true
}
2017-04-04 11:54:52 -04:00
c . SetSiteURLHeader ( app . GetProtocol ( r ) + "://" + r . Host )
2015-06-14 23:53:32 -08:00
w . Header ( ) . Set ( model . HEADER_REQUEST_ID , c . RequestId )
2017-08-16 09:51:45 -07:00
w . Header ( ) . Set ( model . HEADER_VERSION_ID , fmt . Sprintf ( "%v.%v.%v.%v" , model . CurrentVersion , model . BuildNumber , utils . ClientCfgHash , utils . IsLicensed ( ) ) )
2015-07-27 11:59:14 -07:00
2016-08-03 12:19:27 -05:00
// Instruct the browser not to display us in an iframe unless is the same origin for anti-clickjacking
2015-07-27 11:59:14 -07:00
if ! h . isApi {
2016-08-03 12:19:27 -05:00
w . Header ( ) . Set ( "X-Frame-Options" , "SAMEORIGIN" )
w . Header ( ) . Set ( "Content-Security-Policy" , "frame-ancestors 'self'" )
2015-09-11 12:11:10 -04:00
} else {
2015-09-16 15:49:12 -04:00
// All api response bodies will be JSON formatted by default
2015-09-11 12:11:10 -04:00
w . Header ( ) . Set ( "Content-Type" , "application/json" )
2016-02-11 13:17:27 -08:00
if r . Method == "GET" {
w . Header ( ) . Set ( "Expires" , "0" )
}
2015-07-27 11:59:14 -07:00
}
2015-06-14 23:53:32 -08:00
2015-09-16 15:49:12 -04:00
if len ( token ) != 0 {
2017-09-06 17:12:54 -05:00
session , err := app . Global ( ) . GetSession ( token )
2015-06-14 23:53:32 -08:00
2017-01-13 13:53:37 -05:00
if err != nil {
l4g . Error ( utils . T ( "api.context.invalid_session.error" ) , err . Error ( ) )
2015-10-01 17:52:47 -07:00
c . RemoveSessionCookie ( w , r )
2016-02-08 07:26:10 -05:00
if h . requireUser || h . requireSystemAdmin {
2017-09-01 14:58:43 +01:00
c . Err = model . NewAppError ( "ServeHTTP" , "api.context.session_expired.app_error" , nil , "token=" + token , http . StatusUnauthorized )
2016-02-08 07:26:10 -05:00
}
2015-09-16 15:49:12 -04:00
} else if ! session . IsOAuth && isTokenFromQueryString {
2017-09-01 14:58:43 +01:00
c . Err = model . NewAppError ( "ServeHTTP" , "api.context.token_provided.app_error" , nil , "token=" + token , http . StatusUnauthorized )
2015-06-14 23:53:32 -08:00
} else {
c . Session = * session
}
}
2015-07-08 11:50:10 -04:00
if h . isApi || h . isTeamIndependent {
2017-04-04 11:54:52 -04:00
c . setTeamURL ( c . GetSiteURLHeader ( ) , false )
2015-07-08 11:50:10 -04:00
c . Path = r . URL . Path
} else {
splitURL := strings . Split ( r . URL . Path , "/" )
2017-04-04 11:54:52 -04:00
c . setTeamURL ( c . GetSiteURLHeader ( ) + "/" + splitURL [ 1 ] , true )
2015-07-08 11:50:10 -04:00
c . Path = "/" + strings . Join ( splitURL [ 2 : ] , "/" )
}
2017-07-04 16:12:02 -04:00
if h . isApi && ! * utils . Cfg . ServiceSettings . EnableAPIv3 {
c . Err = model . NewAppError ( "ServeHTTP" , "api.context.v3_disabled.app_error" , nil , "" , http . StatusNotImplemented )
}
2015-06-14 23:53:32 -08:00
if c . Err == nil && h . requireUser {
c . UserRequired ( )
}
2016-12-12 08:16:10 -05:00
if c . Err == nil && h . requireMfa {
c . MfaRequired ( )
}
2015-06-14 23:53:32 -08:00
if c . Err == nil && h . requireSystemAdmin {
c . SystemAdminRequired ( )
}
2015-09-16 15:49:12 -04:00
if c . Err == nil && h . isUserActivity && token != "" && len ( c . Session . UserId ) > 0 {
2017-09-06 17:12:54 -05:00
app . Global ( ) . SetStatusOnline ( c . Session . UserId , c . Session . Id , false )
app . Global ( ) . UpdateLastActivityAtIfNeeded ( c . Session )
2015-06-14 23:53:32 -08:00
}
2016-12-20 16:55:22 +01:00
if c . Err == nil && ( h . requireUser || h . requireSystemAdmin ) {
//check if teamId exist
c . CheckTeamId ( )
}
2015-06-14 23:53:32 -08:00
if c . Err == nil {
h . handleFunc ( c , w , r )
}
2016-07-19 12:40:13 -04:00
// Handle errors that have occoured
2015-06-14 23:53:32 -08:00
if c . Err != nil {
2016-01-20 14:36:34 -06:00
c . Err . Translate ( c . T )
2015-06-14 23:53:32 -08:00
c . Err . RequestId = c . RequestId
c . LogError ( c . Err )
c . Err . Where = r . URL . Path
2016-10-19 14:49:25 -04:00
// Block out detailed error when not in developer mode
2016-07-19 12:40:13 -04:00
if ! * utils . Cfg . ServiceSettings . EnableDeveloper {
c . Err . DetailedError = ""
}
2015-06-14 23:53:32 -08:00
if h . isApi {
w . WriteHeader ( c . Err . StatusCode )
w . Write ( [ ] byte ( c . Err . ToJson ( ) ) )
2016-11-22 11:05:54 -08:00
if einterfaces . GetMetricsInterface ( ) != nil {
einterfaces . GetMetricsInterface ( ) . IncrementHttpError ( )
}
2015-06-14 23:53:32 -08:00
} else {
if c . Err . StatusCode == http . StatusUnauthorized {
2015-07-08 11:50:10 -04:00
http . Redirect ( w , r , c . GetTeamURL ( ) + "/?redirect=" + url . QueryEscape ( r . URL . Path ) , http . StatusTemporaryRedirect )
2015-06-14 23:53:32 -08:00
} else {
2017-04-20 09:55:02 -04:00
utils . RenderWebError ( c . Err , w , r )
2015-06-14 23:53:32 -08:00
}
}
2016-11-22 11:05:54 -08:00
}
if h . isApi && einterfaces . GetMetricsInterface ( ) != nil {
2017-01-30 08:30:02 -05:00
if r . URL . Path != model . API_URL_SUFFIX_V3 + "/users/websocket" {
2016-11-22 11:05:54 -08:00
elapsed := float64 ( time . Since ( now ) ) / float64 ( time . Second )
einterfaces . GetMetricsInterface ( ) . ObserveHttpRequestDuration ( elapsed )
}
2015-06-14 23:53:32 -08:00
}
}
func ( c * Context ) LogAudit ( extraInfo string ) {
2015-09-16 15:49:12 -04:00
audit := & model . Audit { UserId : c . Session . UserId , IpAddress : c . IpAddress , Action : c . Path , ExtraInfo : extraInfo , SessionId : c . Session . Id }
2017-09-06 17:12:54 -05:00
if r := <- app . Global ( ) . Srv . Store . Audit ( ) . Save ( audit ) ; r . Err != nil {
2015-08-20 15:04:37 -07:00
c . LogError ( r . Err )
}
2015-06-14 23:53:32 -08:00
}
func ( c * Context ) LogAuditWithUserId ( userId , extraInfo string ) {
2015-08-20 15:04:37 -07:00
if len ( c . Session . UserId ) > 0 {
extraInfo = strings . TrimSpace ( extraInfo + " session_user=" + c . Session . UserId )
}
2015-06-14 23:53:32 -08:00
2015-09-16 15:49:12 -04:00
audit := & model . Audit { UserId : userId , IpAddress : c . IpAddress , Action : c . Path , ExtraInfo : extraInfo , SessionId : c . Session . Id }
2017-09-06 17:12:54 -05:00
if r := <- app . Global ( ) . Srv . Store . Audit ( ) . Save ( audit ) ; r . Err != nil {
2015-08-20 15:04:37 -07:00
c . LogError ( r . Err )
}
2015-06-14 23:53:32 -08:00
}
func ( c * Context ) LogError ( err * model . AppError ) {
2016-07-01 11:57:17 -08:00
// filter out endless reconnects
2016-08-26 13:24:57 -08:00
if c . Path == "/api/v3/users/websocket" && err . StatusCode == 401 || err . Id == "web.check_browser_compatibility.app_error" {
2016-07-01 11:57:17 -08:00
c . LogDebug ( err )
2017-09-01 08:53:55 -05:00
} else if err . Id != "api.post.create_post.town_square_read_only" {
2017-04-28 07:03:52 -07:00
l4g . Error ( utils . TDefault ( "api.context.log.error" ) , c . Path , err . Where , err . StatusCode ,
c . RequestId , c . Session . UserId , c . IpAddress , err . SystemMessage ( utils . TDefault ) , err . DetailedError )
2016-07-01 11:57:17 -08:00
}
2015-06-14 23:53:32 -08:00
}
2016-06-29 04:16:20 -08:00
func ( c * Context ) LogDebug ( err * model . AppError ) {
2017-04-28 07:03:52 -07:00
l4g . Debug ( utils . TDefault ( "api.context.log.error" ) , c . Path , err . Where , err . StatusCode ,
c . RequestId , c . Session . UserId , c . IpAddress , err . SystemMessage ( utils . TDefault ) , err . DetailedError )
2016-06-29 04:16:20 -08:00
}
2015-06-14 23:53:32 -08:00
func ( c * Context ) UserRequired ( ) {
2017-07-31 12:59:32 -04:00
if ! * utils . Cfg . ServiceSettings . EnableUserAccessTokens && c . Session . Props [ model . SESSION_PROP_TYPE ] == model . SESSION_TYPE_USER_ACCESS_TOKEN {
c . Err = model . NewAppError ( "" , "api.context.session_expired.app_error" , nil , "UserAccessToken" , http . StatusUnauthorized )
return
}
2015-06-14 23:53:32 -08:00
if len ( c . Session . UserId ) == 0 {
2017-07-31 12:59:32 -04:00
c . Err = model . NewAppError ( "" , "api.context.session_expired.app_error" , nil , "UserRequired" , http . StatusUnauthorized )
2015-06-14 23:53:32 -08:00
return
}
}
2016-12-12 08:16:10 -05:00
func ( c * Context ) MfaRequired ( ) {
// Must be licensed for MFA and have it configured for enforcement
2017-08-16 09:51:45 -07:00
if ! utils . IsLicensed ( ) || ! * utils . License ( ) . Features . MFA || ! * utils . Cfg . ServiceSettings . EnableMultifactorAuthentication || ! * utils . Cfg . ServiceSettings . EnforceMultifactorAuthentication {
2016-12-12 08:16:10 -05:00
return
}
// OAuth integrations are excepted
if c . Session . IsOAuth {
return
}
2017-09-06 17:12:54 -05:00
if result := <- app . Global ( ) . Srv . Store . User ( ) . Get ( c . Session . UserId ) ; result . Err != nil {
2017-09-01 14:58:43 +01:00
c . Err = model . NewAppError ( "" , "api.context.session_expired.app_error" , nil , "MfaRequired" , http . StatusUnauthorized )
2016-12-12 08:16:10 -05:00
return
} else {
user := result . Data . ( * model . User )
// Only required for email and ldap accounts
if user . AuthService != "" &&
user . AuthService != model . USER_AUTH_SERVICE_EMAIL &&
user . AuthService != model . USER_AUTH_SERVICE_LDAP {
return
}
if ! user . MfaActive {
2017-09-01 14:58:43 +01:00
c . Err = model . NewAppError ( "" , "api.context.mfa_required.app_error" , nil , "MfaRequired" , http . StatusUnauthorized )
2016-12-12 08:16:10 -05:00
return
}
}
}
2015-06-14 23:53:32 -08:00
func ( c * Context ) SystemAdminRequired ( ) {
if len ( c . Session . UserId ) == 0 {
2017-09-01 14:58:43 +01:00
c . Err = model . NewAppError ( "" , "api.context.session_expired.app_error" , nil , "SystemAdminRequired" , http . StatusUnauthorized )
2015-06-14 23:53:32 -08:00
return
2017-01-30 08:30:02 -05:00
} else if ! c . IsSystemAdmin ( ) {
2017-09-01 14:58:43 +01:00
c . Err = model . NewAppError ( "" , "api.context.permissions.app_error" , nil , "AdminRequired" , http . StatusForbidden )
2015-06-14 23:53:32 -08:00
return
}
}
2017-01-30 08:30:02 -05:00
func ( c * Context ) IsSystemAdmin ( ) bool {
return app . SessionHasPermissionTo ( c . Session , model . PERMISSION_MANAGE_SYSTEM )
}
2015-10-01 17:52:47 -07:00
func ( c * Context ) RemoveSessionCookie ( w http . ResponseWriter , r * http . Request ) {
2015-10-20 14:49:42 -07:00
cookie := & http . Cookie {
2015-10-20 04:37:51 -07:00
Name : model . SESSION_COOKIE_TOKEN ,
2015-10-20 14:49:42 -07:00
Value : "" ,
2015-10-01 17:52:47 -07:00
Path : "/" ,
2015-10-20 14:49:42 -07:00
MaxAge : - 1 ,
2015-10-01 17:52:47 -07:00
HttpOnly : true ,
}
2017-05-04 16:36:31 -04:00
userCookie := & http . Cookie {
Name : model . SESSION_COOKIE_USER ,
Value : "" ,
Path : "/" ,
MaxAge : - 1 ,
}
2015-10-20 14:49:42 -07:00
http . SetCookie ( w , cookie )
2017-05-04 16:36:31 -04:00
http . SetCookie ( w , userCookie )
2015-06-14 23:53:32 -08:00
}
func ( c * Context ) SetInvalidParam ( where string , name string ) {
2016-05-05 16:35:03 -04:00
c . Err = NewInvalidParamError ( where , name )
}
func NewInvalidParamError ( where string , name string ) * model . AppError {
2017-09-01 14:58:43 +01:00
err := model . NewAppError ( where , "api.context.invalid_param.app_error" , map [ string ] interface { } { "Name" : name } , "" , http . StatusBadRequest )
2016-05-05 16:35:03 -04:00
return err
2015-06-14 23:53:32 -08:00
}
func ( c * Context ) SetUnknownError ( where string , details string ) {
2017-09-01 14:58:43 +01:00
c . Err = model . NewAppError ( where , "api.context.unknown.app_error" , nil , details , http . StatusInternalServerError )
2015-06-14 23:53:32 -08:00
}
2017-01-23 08:12:05 -05:00
func ( c * Context ) SetPermissionError ( permission * model . Permission ) {
2017-09-01 14:58:43 +01:00
c . Err = model . NewAppError ( "Permissions" , "api.context.permissions.app_error" , nil , "userId=" + c . Session . UserId + ", " + "permission=" + permission . Id , http . StatusForbidden )
2017-01-23 08:12:05 -05:00
}
2015-07-08 11:50:10 -04:00
func ( c * Context ) setTeamURL ( url string , valid bool ) {
c . teamURL = url
c . teamURLValid = valid
}
2015-10-01 17:52:47 -07:00
func ( c * Context ) SetTeamURLFromSession ( ) {
2017-09-06 17:12:54 -05:00
if result := <- app . Global ( ) . Srv . Store . Team ( ) . Get ( c . TeamId ) ; result . Err == nil {
2017-04-04 11:54:52 -04:00
c . setTeamURL ( c . GetSiteURLHeader ( ) + "/" + result . Data . ( * model . Team ) . Name , true )
2015-07-08 11:50:10 -04:00
}
}
2017-04-04 11:54:52 -04:00
func ( c * Context ) SetSiteURLHeader ( url string ) {
c . siteURLHeader = strings . TrimRight ( url , "/" )
2016-08-09 09:53:22 -04:00
}
2017-04-04 11:54:52 -04:00
// TODO see where these are used
2015-07-08 11:50:10 -04:00
func ( c * Context ) GetTeamURLFromTeam ( team * model . Team ) string {
2017-04-04 11:54:52 -04:00
return c . GetSiteURLHeader ( ) + "/" + team . Name
2015-07-08 11:50:10 -04:00
}
func ( c * Context ) GetTeamURL ( ) string {
if ! c . teamURLValid {
2015-10-01 17:52:47 -07:00
c . SetTeamURLFromSession ( )
2015-07-08 11:50:10 -04:00
if ! c . teamURLValid {
2016-01-21 10:07:29 -03:00
l4g . Debug ( utils . T ( "api.context.invalid_team_url.debug" ) )
2015-07-08 11:50:10 -04:00
}
}
return c . teamURL
}
2017-04-04 11:54:52 -04:00
func ( c * Context ) GetSiteURLHeader ( ) string {
return c . siteURLHeader
2015-07-08 11:50:10 -04:00
}
2016-09-13 12:42:48 -04:00
func ( c * Context ) GetCurrentTeamMember ( ) * model . TeamMember {
return c . Session . GetTeamByTeamId ( c . TeamId )
}
2016-04-21 22:37:01 -07:00
func IsApiCall ( r * http . Request ) bool {
return strings . Index ( r . URL . Path , "/api/" ) == 0
}
2015-06-14 23:53:32 -08:00
func Handle404 ( w http . ResponseWriter , r * http . Request ) {
2017-09-01 14:58:43 +01:00
err := model . NewAppError ( "Handle404" , "api.context.404.app_error" , nil , "" , http . StatusNotFound )
2016-04-21 22:37:01 -07:00
err . Translate ( utils . T )
2016-06-29 04:16:20 -08:00
2017-01-13 13:53:37 -05:00
l4g . Debug ( "%v: code=404 ip=%v" , r . URL . Path , utils . GetIpAddress ( r ) )
2016-04-21 22:37:01 -07:00
if IsApiCall ( r ) {
w . WriteHeader ( err . StatusCode )
err . DetailedError = "There doesn't appear to be an api call for the url='" + r . URL . Path + "'. Typo? are you missing a team_id or user_id as part of the url?"
w . Write ( [ ] byte ( err . ToJson ( ) ) )
} else {
2017-04-20 09:55:02 -04:00
utils . RenderWebError ( err , w , r )
2016-04-21 22:37:01 -07:00
}
2015-06-14 23:53:32 -08:00
}
2015-09-16 15:49:12 -04:00
2016-12-20 16:55:22 +01:00
func ( c * Context ) CheckTeamId ( ) {
if c . TeamId != "" && c . Session . GetTeamByTeamId ( c . TeamId ) == nil {
2017-01-23 08:12:05 -05:00
if app . SessionHasPermissionTo ( c . Session , model . PERMISSION_MANAGE_SYSTEM ) {
2017-09-06 17:12:54 -05:00
if result := <- app . Global ( ) . Srv . Store . Team ( ) . Get ( c . TeamId ) ; result . Err != nil {
2016-12-20 16:55:22 +01:00
c . Err = result . Err
c . Err . StatusCode = http . StatusBadRequest
return
}
} else {
2017-01-23 08:12:05 -05:00
c . SetPermissionError ( model . PERMISSION_MANAGE_SYSTEM )
2016-12-20 16:55:22 +01:00
return
}
}
}