Account.java 4.43 KB
Newer Older
Melledy's avatar
Melledy committed
1
2
package emu.grasscutter.game;

3
import dev.morphia.annotations.*;
AnimeGitB's avatar
AnimeGitB committed
4
import emu.grasscutter.Grasscutter;
Melledy's avatar
Melledy committed
5
6
7
8
import emu.grasscutter.database.DatabaseHelper;
import emu.grasscutter.utils.Crypto;
import emu.grasscutter.utils.Utils;

Melledy's avatar
Melledy committed
9
import java.util.ArrayList;
10
import java.util.List;
Secretboy's avatar
Secretboy committed
11
import java.util.Locale;
12

13
14
import org.bson.Document;

15
import static emu.grasscutter.Configuration.*;
16

KingRainbow44's avatar
KingRainbow44 committed
17
@Entity(value = "accounts", useDiscriminator = false)
Melledy's avatar
Melledy committed
18
19
20
21
22
23
24
25
public class Account {
	@Id private String id;
	
	@Indexed(options = @IndexOptions(unique = true))
	@Collation(locale = "simple", caseLevel = true)
	private String username;
	private String password; // Unused for now
	
26
	@AlsoLoad("playerUid") private int playerId;
Melledy's avatar
Melledy committed
27
28
29
30
	private String email;
	
	private String token;
	private String sessionKey; // Session token for dispatch server
31
	private List<String> permissions;
Secretboy's avatar
Secretboy committed
32
    private Locale locale;
33
	
Melledy's avatar
Melledy committed
34
	@Deprecated
Melledy's avatar
Melledy committed
35
36
	public Account() {
		this.permissions = new ArrayList<>();
37
        this.locale = LANGUAGE;
Melledy's avatar
Melledy committed
38
	}
Melledy's avatar
Melledy committed
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

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getToken() {
		return token;
	}

	public void setToken(String token) {
		this.token = token;
	}

72
	public int getPlayerUid() {
Melledy's avatar
Melledy committed
73
74
75
76
77
78
79
80
		return this.playerId;
	}

	public void setPlayerId(int playerId) {
		this.playerId = playerId;
	}
	
	public String getEmail() {
4Benj_'s avatar
4Benj_ committed
81
82
83
84
85
		if(email != null && !email.isEmpty()) {
			return email;
		} else {
			return "";
		}
Melledy's avatar
Melledy committed
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getSessionKey() {
		return this.sessionKey;
	}

	public String generateSessionKey() {
		this.sessionKey = Utils.bytesToHex(Crypto.createSessionKey(32));
		this.save();
		return this.sessionKey;
	}
101

Secretboy's avatar
Secretboy committed
102
103
104
105
106
107
108
109
    public Locale getLocale() {
        return locale;
    }

    public void setLocale(Locale locale) {
        this.locale = locale;
    }

110
111
112
113
114
115
116
117
118
119
120
	/**
	 * The collection of a player's permissions.
	 */
	public List<String> getPermissions() {
		return this.permissions;
	}
	
	public boolean addPermission(String permission) {
		if(this.permissions.contains(permission)) return false;
		this.permissions.add(permission); return true;
	}
121

AnimeGitB's avatar
AnimeGitB committed
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
	public static boolean permissionMatchesWildcard(String wildcard, String[] permissionParts) {
		String[] wildcardParts = wildcard.split("\\.");
		if (permissionParts.length < wildcardParts.length) {  // A longer wildcard can never match a shorter permission
			return false;
		}
		for (int i=0; i<wildcardParts.length; i++) {
			switch (wildcardParts[i]) {
				case "**":  // Recursing match
					return true;
				case "*":  // Match only one layer
					if (i >= (permissionParts.length-1)) {
						return true;
					}
					break;
				default:  // This layer isn't a wildcard, it needs to match exactly
					if (!wildcardParts[i].equals(permissionParts[i])) {
						return false;
					}
			}
		}
		// At this point the wildcard will have matched every layer, but if it is shorter then the permission then this is not a match at this point (no **).
		return (wildcardParts.length == permissionParts.length);
	}

146
	public boolean hasPermission(String permission) {
muhammadeko's avatar
muhammadeko committed
147
148
149

		if (this.permissions.contains(permission)) return true;
		if(this.permissions.contains("*") && this.permissions.size() == 1) return true;
muhammadeko's avatar
muhammadeko committed
150

AnimeGitB's avatar
AnimeGitB committed
151
152
		String[] permissionParts = permission.split("\\.");
		for (String p : this.permissions) {
muhammadeko's avatar
muhammadeko committed
153
			if (p.startsWith("-") && permissionMatchesWildcard(p.substring(1), permissionParts)) return false;
muhammadeko's avatar
muhammadeko committed
154
			if (permissionMatchesWildcard(p, permissionParts)) return true;
AnimeGitB's avatar
AnimeGitB committed
155
		}
muhammadeko's avatar
muhammadeko committed
156
157

		return this.permissions.contains("*");
158
	}
159
160
161
162
	
	public boolean removePermission(String permission) {
		return this.permissions.remove(permission);
	}
Melledy's avatar
Melledy committed
163
164
165
166
167
168
169
170
171
172
173
	
	// TODO make unique
	public String generateLoginToken() {
		this.token = Utils.bytesToHex(Crypto.createSessionKey(32));
		this.save();
		return this.token;
	}
	
	public void save() {
		DatabaseHelper.saveAccount(this);
	}
174

175
	@PreLoad
176
	public void onLoad(Document document) {
177
		// Grant the superuser permissions to accounts created before the permissions update
178
		if (!document.containsKey("permissions")) {
179
180
			this.addPermission("*");
		}
Secretboy's avatar
Secretboy committed
181
182
183

        // Set account default language as server default language
        if (!document.containsKey("locale")) {
184
            this.locale = LANGUAGE;
Secretboy's avatar
Secretboy committed
185
        }
186
	}
Melledy's avatar
Melledy committed
187
}