aboutsummaryrefslogtreecommitdiff
path: root/Plugins.Essentials/src/Extensions/UserExtensions.cs
blob: 9223b1de88a6d3641cf76dcdf9e8ec94807699a1 (plain)
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
/*
* Copyright (c) 2022 Vaughn Nugent
* 
* Library: VNLib
* Package: VNLib.Plugins.Essentials
* File: UserExtensions.cs 
*
* UserExtensions.cs is part of VNLib.Plugins.Essentials which is part of the larger 
* VNLib collection of libraries and utilities.
*
* VNLib.Plugins.Essentials is free software: you can redistribute it and/or modify 
* it under the terms of the GNU Affero General Public License as 
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* VNLib.Plugins.Essentials is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program.  If not, see https://www.gnu.org/licenses/.
*/

using System;
using System.Text.Json;

using VNLib.Plugins.Essentials.Users;
using VNLib.Plugins.Essentials.Accounts;

#nullable enable

namespace VNLib.Plugins.Essentials.Extensions
{
    /// <summary>
    /// Provides extension methods to the Users namespace
    /// </summary>
    public static class UserExtensions
    {
        
        private const string PROFILE_ENTRY = "__.prof";

        /// <summary>
        /// Stores the user's profile to their entry. 
        /// <br/>
        /// NOTE: You must validate/filter data before storing
        /// </summary>
        /// <param name="ud"></param>
        /// <param name="profile">The profile object to store on account</param>
        /// <exception cref="JsonException"></exception>
        /// <exception cref="NotSupportedException"></exception>
        public static void SetProfile(this IUser ud, AccountData? profile)
        {
            //Clear entry if its null
            if (profile == null)
            {
                ud[PROFILE_ENTRY] = null!;
                return;
            }
            //Dont store duplicate values
            profile.Created = null;
            profile.EmailAddress = null;
            ud.SetObject(PROFILE_ENTRY, profile);
        }
        /// <summary>
        /// Stores the serialized string user's profile to their entry.
        /// <br/>
        /// NOTE: No data validation checks are performed
        /// </summary>
        /// <param name="ud"></param>
        /// <param name="jsonProfile">The JSON serialized "raw" profile data</param>
        public static void SetProfile(this IUser ud, string jsonProfile) => ud[PROFILE_ENTRY] = jsonProfile;
        /// <summary>
        /// Recovers the user's stored profile
        /// </summary> 
        /// <returns>The user's profile stored in the entry or null if no entry is found</returns>
        /// <exception cref="JsonException"></exception>
        /// <exception cref="NotSupportedException"></exception>
        public static AccountData? GetProfile(this IUser ud)
        {
            //Recover profile data, or create new empty profile data
            AccountData? ad = ud.GetObject<AccountData>(PROFILE_ENTRY);
            if (ad == null)
            {
                return null;
            }
            //Set email the same as the account
            ad.EmailAddress = ud.EmailAddress;
            //Store the rfc time
            ad.Created = ud.Created.ToString("R");
            return ad;
        }
    }
}