Skip to content
Snippets Groups Projects
Select Git revision
  • ed6d04da1cfb341c9ab81b1372626c792ba22ec1
  • main default protected
  • feature/export-filtering
  • feature/clear-schedule-button
  • fix/responsive-cols-in-polls
  • feature/preference-polling-form
  • feature/json-export-via-rest-framework
  • feature/json-schedule-import-tests
  • fix/add-room-import-only-once
  • ak-import
  • renovate/django-simple-history-3.x
  • renovate/django-debug-toolbar-4.x
  • renovate/django-5.x
  • renovate/mysqlclient-2.x
14 results

site.py

Blame
  • Forked from KIF / AKPlanning
    Source project has a limited visibility.
    Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    bugreport.go 2.23 KiB
    package wtfd
    
    import (
    	"errors"
    	"fmt"
    	"time"
    	"strconv"
    	"net/smtp"
    )
    
    
    var (
    	/* Defaults */
    	BRServiceDeskDomain          = "example.com" // Server recieving service desk mails
    	BRServiceDeskUser            = "noreply"     // The user recieving the mails
    	BRServiceDeskPort            = 25            // server to server smtp port
    	BRServiceDeskEnabled         = false         // Is service desk support enabled
    	BRRateLimitInterval  float64 = 180           // 3 Minutes
    	BRRateLimitReports           = 2             // 2 Reports during interval before beeing rate limited
    
    	userAccess map[string]access = make(map[string]access)
    )
    
    type access struct {
    	lastBlock  time.Time // Currently unused
    	lastAccess []time.Time
    }
    
    
    /**
     * Check if user is rate limited
     */
    func BRIsUserRateLimited(u *User) bool {
    	record, ok := userAccess[u.Name]
    	if !ok {
    		return false
    	}
    
    	/* Ok if no critical ammount of records */
    	if len(record.lastAccess) < BRRateLimitReports {
    		return false
    	}
    
    	/* Check if earliest record is in interval, then block */
    	if time.Since(record.lastAccess[0]).Seconds() < BRRateLimitInterval {
    		return true
    	}
    
    	return false
    }
    
    /**
     * Register a user access
     */
    func registerUserAccess(u *User) {
    	record, ok := userAccess[u.Name]
    
    	if !ok {
    		/* New record */
    		record = access{
    			lastBlock:  time.Time{},
    			lastAccess: []time.Time{time.Now()},
    		}
    	} else if len(record.lastAccess) < BRRateLimitReports {
    		/* No critical ammount of records */
    		record.lastAccess = append(record.lastAccess, time.Now())
    	} else if !BRIsUserRateLimited(u) {
    		/* Cycle access */
    		record.lastAccess = record.lastAccess[1:]
    		record.lastAccess = append(record.lastAccess, time.Now())
    	}
    	userAccess[u.Name] = record
    }
    
    /**
     * Send bugreport
     */
    func BRDispatchBugreport(u *User, subject string, content string) error {
    	if !BRServiceDeskEnabled {
    		return errors.New("Service Desk is disabled")
    	}
    
    	recipient := BRServiceDeskUser + "@" + BRServiceDeskDomain
    	recipients := []string{recipient}
    	formatContent := fmt.Sprintf("From: %s\nSubject: %s\n\n%s", u.Name, subject, content)
    
    
    	err := smtp.SendMail(BRServiceDeskDomain+":"+strconv.Itoa(BRServiceDeskPort),
    		nil, u.Name, recipients, []byte(formatContent))
    	if err == nil {
    		registerUserAccess(u)
    	}
    	return err
    }