File size: 3,676 Bytes
afc0068
 
 
 
a6bca2b
4c36de1
afc0068
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4c36de1
a6bca2b
62e311a
4c36de1
afc0068
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4c36de1
a6bca2b
62e311a
afc0068
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b25b478
 
 
 
 
 
 
afc0068
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/**
 * Authentication Manager Module
 * Handles user authentication, session management, and permissions
 */
// import { backgroundDataService } from './background-data.js'; // Temporarily disabled

export class AuthManager {
    constructor() {
        this.currentUser = null;
        this.authToken = null;
    }

    async checkAuthentication() {
        const token = localStorage.getItem('auth_token');
        if (!token) {
            return false;
        }
        
        try {
            const response = await fetch('/api/auth/validate', {
                headers: {
                    'Authorization': `Bearer ${token}`
                }
            });
            
            if (response.ok) {
                const data = await response.json();
                this.currentUser = data.user;
                this.authToken = token;
                
                // Background data loading temporarily disabled
                console.log('Authentication successful - background loading disabled for now');
                
                return true;
            } else {
                this.clearAuthData();
                return false;
            }
        } catch (error) {
            console.error('Auth validation error:', error);
            return false;
        }
    }

    async logout() {
        try {
            await fetch('/api/auth/logout', {
                method: 'POST',
                headers: {
                    'Authorization': `Bearer ${this.authToken}`
                }
            });
        } catch (error) {
            console.error('Logout error:', error);
        } finally {
            this.clearAuthData();
            window.location.href = '/login';
        }
    }

    clearAuthData() {
        localStorage.removeItem('auth_token');
        localStorage.removeItem('user_info');
        this.currentUser = null;
        this.authToken = null;
        
        // Background data cache clearing temporarily disabled
        console.log('Auth data cleared');
    }

    canEditTree(createdBy) {
        if (!this.currentUser) return false;
        
        const permissions = this.currentUser.permissions || [];
        
        // Admin and system can edit any tree
        if (permissions.includes('admin') || permissions.includes('system')) {
            return true;
        }
        
        // Users can edit trees they created
        if (permissions.includes('edit_own') && createdBy === this.currentUser.username) {
            return true;
        }
        
        // Users with delete permission can edit any tree
        if (permissions.includes('delete')) {
            return true;
        }
        
        return false;
    }
    
    canDeleteTree(createdBy) {
        if (!this.currentUser) return false;
        
        const permissions = this.currentUser.permissions || [];
        
        // Only admin and system can delete trees
        if (permissions.includes('admin') || permissions.includes('system')) {
            return true;
        }
        
        // Users with explicit delete permission
        if (permissions.includes('delete')) {
            return true;
        }
        
        return false;
    }

    getAuthHeaders() {
        return {
            'Content-Type': 'application/json',
            'Authorization': `Bearer ${this.authToken}`
        };
    }

    isDemoUser() {
        if (!this.currentUser) return false;
        return this.currentUser.role === 'demo_user' || 
               this.currentUser.username === 'demo_user' ||
               (this.currentUser.permissions && this.currentUser.permissions.includes('demo_view'));
    }
}