49 lines
1.8 KiB
C#
49 lines
1.8 KiB
C#
using System;
|
|
|
|
namespace bts {
|
|
public readonly struct UserTuple : IEquatable<UserTuple>, IComparable<UserTuple>, IComparable {
|
|
const string _admin_name = "bts";
|
|
|
|
public static readonly Func<UserTuple, uint> key_fn = u => u.id;
|
|
public static UserTuple admin => new UserTuple(0, _admin_name, Permissions.Admin);
|
|
|
|
public readonly uint id;
|
|
public readonly string name;
|
|
public readonly Permissions permissions;
|
|
|
|
public UserTuple(uint id, string name)
|
|
: this(id, name, name == _admin_name ? Permissions.Admin : Permissions.Regular) { }
|
|
|
|
public UserTuple(uint id, string name, Permissions permissions) {
|
|
this.id = id;
|
|
this.name = name;
|
|
this.permissions = permissions;
|
|
}
|
|
|
|
public enum Permissions {
|
|
Regular,
|
|
Admin
|
|
}
|
|
|
|
public int CompareTo(UserTuple other) {
|
|
int comp = string.Compare(name, other.name);
|
|
return comp == 0 ? id.CompareTo(other.id) : comp;
|
|
}
|
|
|
|
int IComparable.CompareTo(object obj) =>
|
|
obj is UserTuple other ? CompareTo(other) : 1;
|
|
|
|
public bool Equals(UserTuple other) => other.id == id && other.name == name;
|
|
public override bool Equals(object obj) => obj is UserTuple other && Equals(other);
|
|
public override int GetHashCode() => id.GetHashCode();
|
|
public override string ToString() => name;
|
|
|
|
public static bool operator ==(UserTuple a, UserTuple b) => a.Equals(b);
|
|
public static bool operator !=(UserTuple a, UserTuple b) => !a.Equals(b);
|
|
}
|
|
|
|
public static class UserTupleExtensions {
|
|
public static bool can_access_user_data(this UserTuple user, uint user_id) =>
|
|
user.id == user_id || user.permissions == UserTuple.Permissions.Admin;
|
|
}
|
|
} |