summaryrefslogtreecommitdiff
path: root/Project-Unite/Models/IdentityModels.cs
blob: 8d368ed9d9a6741fb93839fcba760343d5a90085 (plain) (blame)
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using System.Web.Mvc;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;

namespace Project_Unite.Models
{
    // You can add profile data for the user by adding more properties to your ApplicationUser class, please visit http://go.microsoft.com/fwlink/?LinkID=317594 to learn more.
    public class ApplicationUser : IdentityUser
    {
        public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
        {
            // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
            var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
            // Add custom user claims here
            return userIdentity;
            
        }

        public Role HighestRole
        {
            get
            {
                var roleList = new List<Role>();
                foreach (var role in this.Roles)
                {
                    roleList.Add(new ApplicationDbContext().Roles.First(r => r.Id == role.RoleId) as Role);

                }
                return roleList.OrderByDescending(x => x.Priority).First();
            }
        }

        public string LastKnownIPAddress { get; set; }
        public DateTime JoinedAt { get; set; }
        public DateTime LastLogin { get; set; }
        
        public bool IsBanned { get; set; }
        public bool IsMuted { get; set; }

        public DateTime BannedAt { get; set; }
        public DateTime MutedAt { get; set; }
        public string BannedBy { get; set; }
        public string MutedBy { get; set; }

        public int PostCount
        {
            get
            {
                using(var db = new ApplicationDbContext())
                {
                    return db.ForumPosts.Where(x=>x.AuthorId == this.Id).Count();
                }
            }
        }

        public int TopicCount
        {
            get
            {
                using (var db = new ApplicationDbContext())
                {
                    return db.ForumTopics.Where(x => x.AuthorId == this.Id).Count();
                }
            }
        }

        public long Codepoints { get; set; }

        [AllowHtml]
        public string Bio { get; set; }

        public string BannerUrl { get; set; }
        public string AvatarUrl { get; set; }

        public string DisplayName { get; set; }
        public string FullName { get; set; }
        public string Website { get; set; }
        public string YoutubeUrl { get; set; }
        public string SystemName { get; set; }
        
        public string Interests { get; set; }
        public string Hobbies { get; set; }
        

        public UserPost[] Posts
        {
            get
            {
                var db = new ApplicationDbContext();
                return db.UserPosts.Where(x => x.UserId == this.Id).ToArray();
            }
        }
    }

    public class BannedIP
    {
        public string Id { get; set; }
        public string Address { get; set; }
    }

    public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
        public ApplicationDbContext()
            : base("DefaultConnection", throwIfV1Schema: false)
        {
            
        }

        public void DeleteObject(object obj)
        {
            ((IObjectContextAdapter)this).ObjectContext.DeleteObject(obj);
        }

        public static ApplicationDbContext Create()
        {
            return new ApplicationDbContext();
        }

        public DbSet<UserPost> UserPosts { get; set; }
        public DbSet<ForumPostEdit> ForumPostEdits { get; set; }
        public DbSet<Like> Likes { get; set; }
        public DbSet<ForumPermission> ForumPermissions { get; set; }
        public DbSet<BannedIP> BannedIPs { get; set; }
        public DbSet<AuditLog> AuditLogs { get; set; }
        public System.Data.Entity.DbSet<Project_Unite.Models.Role> IdentityRoles { get; set; }
        public DbSet<ForumCategory> ForumCategories { get; set; }
        public DbSet<ForumTopic> ForumTopics { get; set; }
        public DbSet<ForumPoll> ForumPolls { get; set; }
        public DbSet<ForumPollOption> ForumPollOptions { get; set; }
        public DbSet<ForumPollVote> ForumPollVotes { get; set; }
        public DbSet<ForumPost> ForumPosts { get; set; }
        
    }

    public class UserPost
    {
        public string Id { get; set; }
        public string UserId { get; set; }

        [MaxLength(1000, ErrorMessage ="Your post can't have more than 1000 characters.")]
        [AllowHtml]
        [MinLength(20, ErrorMessage ="To prevent spam, you must have at least 20 characters in your post.")]
        public string PostContents { get; set; }

        public DateTime PostedAt { get; set; }

        public Like[] Likes
        {
            get
            {
                return new ApplicationDbContext().Likes.Where(l => l.Topic == this.Id).Where(x => x.IsDislike == false).ToArray();
            }
        }

        public Like[] Dislikes
        {
            get
            {
                return new ApplicationDbContext().Likes.Where(l => l.Topic == this.Id).Where(x => x.IsDislike == true).ToArray();
            }
        }

    }
}