Exemplo n.º 1
0
 /// <summary>
 /// Deauthorize the facebook user on our application (signout)
 /// </summary>
 public static void Deauthorize()
 {
     try
     {
         var ticket = ManageUserModelHelper.GetFormsAuthenticationTicket();
         if (ticket != null)
         {
             var idKey = ticket.IdKey();
             if (idKey != Guid.Empty)
             {
                 using (var db = new DbContextHelper())
                 {
                     User user = ManageUserModelHelper.GetUser(db, idKey);
                     if (user != null)
                     {
                         user.UserStatusEnum = UserStatus.Offline;
                         db.SaveChanges();
                     }
                 } //end using
             } //end if
         } //end if
         FormsAuthentication.SignOut();
     }
     catch (Exception ex)
     {
         LogHelper.LogFatalError("FacebookHelper.Deauthorize", ex);
         throw ex;
     }
 }
        /// <summary>
        /// Creates a paginated entity model used for the MinistriesController.Index view
        /// </summary>
        /// <param name="model">Pagination model for ministries</param>
        /// <param name="pageNumber">The page number to return</param>
        /// <returns></returns>
        public static void GetPaginatedMinistries(this PaginationModel<MinistryModel> model, int pageNumber = 1)
        {
            try
            {
                using (var db = new DbContextHelper())
                {
                    var data = (from m in db.Ministries
                                  orderby m.Name
                                  select new MinistryModel
                                  {
                                      Id = m.Id,
                                      IdKey = m.IdKey,
                                      Name = m.Name,
                                      Description = m.Description,
                                  })
                                .OrderBy(model.SortOptions.Column, model.SortOptions.Direction)
                                .ToList();

                    model.Data = new CustomPagination<MinistryModel>(data, pageNumber, 20, data.Count);
                }
            }
            catch (Exception ex)
            {
                LogHelper.LogFatalError("ManageMinistryModelHelper.GetPaginatedMinistries", ex);
                throw ex;
            }
        }
Exemplo n.º 3
0
        public static List<QuoteModel> GetAll()
        {
            List<QuoteModel> quotes = new List<QuoteModel>();

            try
            {
                using (var db = new DbContextHelper())
                {
                    quotes = (from q in db.Quotes
                              where q.Approved == true
                              select new QuoteModel { 
                                  Id = q.Id, 
                                  Description = q.Description, 
                                  Source = q.Source 
                              }).ToList();

                    return quotes;
                }
            }
            catch (Exception ex)
            {
                LogHelper.LogFatalError("QuoteModelHelper.GetRandom", ex);
                throw ex;
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Creates a paginated quote model used for the QuoteController.Index view
        /// </summary>
        /// <param name="model">Paginated model for quotes</param>
        /// <param name="pageNumber">The page number to return</param>
        /// <returns></returns>
        public static void GetPaginatedQuotes(this PaginationModel<QuoteModel> model, int pageNumber = 1)
        {
            try
            {
                using (var db = new DbContextHelper())
                {
                    var data = (from q in db.Quotes
                                  select new QuoteModel
                                  {
                                      Id = q.Id,
                                      IdKey = q.IdKey,
                                      Description = q.Description,
                                      Source = q.Source,
                                      Approved = q.Approved,
                                  })
                                .OrderBy(model.SortOptions.Column, model.SortOptions.Direction)
                                .ToList();

                    model.Data = new CustomPagination<QuoteModel>(data, pageNumber, 20, data.Count);
                }
            }
            catch (Exception ex)
            {
                LogHelper.LogFatalError("ManageQuoteModelHelper.GetPaginatedQuotes", ex);
                throw ex;
            }
        }
        public static bool Add(this MinistryModel model, out Status status)
        {
            try
            {
                using (var db = new DbContextHelper())
                {
                    var entity = db.Ministries.Create();
                    entity.IdKey = Guid.NewGuid();
                    entity.Name = model.Name;
                    entity.Description = model.Description.Sanitize();
                    entity.Created = DateTime.Now;

                    entity = db.Ministries.Add(entity);
                    db.SaveChanges();

                    model.Id = entity.Id;
                    model.IdKey = entity.IdKey;

                    status = Status.Success;
                    return true;
                }
            }
            catch (Exception ex)
            {
                LogHelper.LogFatalError("Manage.MinistryModelHelper.Add", ex);
                status = Status.SystemException;
            }

            return false;
        }
        /// <summary>
        /// Creates a paginated entity model used for the ArticlesController.Index view
        /// </summary>
        /// <param name="model">Paginated model of acticles</param>
        /// <param name="pageNumber">The page number to return</param>
        /// <param name="start">The start date for querying the articles</param>
        /// <param name="end">The end date for querying the articles</param>
        /// <param name="checkIfApproved">Flag to check if articles approved for viewing</param>
        /// <returns></returns>
        public static void GetPaginatedArticles(this PaginationModel<ArticleModel> model, int pageNumber = 1, DateTime? start = null, DateTime? end = null, bool checkIfApproved = true)
        {
            try
            {
                using (var db = new DbContextHelper())
                {
                    var query = (from a in db.Articles select a)
                                .OrderBy(model.SortOptions.Column, model.SortOptions.Direction);

                    if (checkIfApproved)
                        query = query.Where(a => a.Approved == true);

                    if(start.HasValue)
                    {
                        var sDate = start.Value.StartOfDay();
                        query = query.Where(a => a.Start >= sDate);
                    }

                    if (end.HasValue)
                    {
                        var eDate = end.Value.EndOfDay();
                        query = query.Where(a => a.End <= eDate);
                    }

                    var articles = query.ToList();
                    var data = new List<ArticleModel>();
                    foreach (var a in articles)
                    {
                        data.Add(new ArticleModel
                                 {
                                     Id = a.Id,
                                     IdKey = a.IdKey,
                                     Title = a.Title,
                                     Description = a.Description,
                                     StartDate = a.Start.ToShortDateString(),
                                     EndDate = a.End.ToShortDateString(),
                                     Keywords = a.Keywords,
                                     Author = a.Author,
                                     Summary = a.Summary,
                                     Approved = a.Approved,
                                     CategoryId = a.CategoryId,
                                 });
                    }

                    model.Data = new CustomPagination<ArticleModel>(data, pageNumber, 20, data.Count);
                }
            }
            catch (Exception ex)
            {
                LogHelper.LogFatalError("ManageArticleModelHelper.GetPaginatedArticles", ex);
                throw ex;
            }
        }
Exemplo n.º 7
0
        public static Role GetRole(int id, DbContextHelper db)
        {
            try
            {
                var role = (from r in db.Roles
                            where r.Id == id || r.Id == Role.BASIC_ID
                            orderby r.Id descending
                            select r).FirstOrDefault();

                return role;
            }
            catch (Exception ex)
            {
                LogHelper.LogFatalError("ManageRoleModelHelper.GetRole", ex);
                throw ex;
            }
        }
Exemplo n.º 8
0
        public static bool Add(this UserModel model, out Status status)
        {
            try
            {
                if (model.Roles.Count == 0)
                {
                    status = Status.RoleRequired;
                    return(false);
                }

                if (model.EmailExists())
                {
                    status = Status.DuplicateEmail;
                    return(false);
                }

                if (model.ScreenNameExists())
                {
                    status = Status.DuplicateScreenName;
                    return(false);
                }

                using (var db = new DbContextHelper())
                {
                    var user = CreateNewUserFromModel(model, db);
                    user.UserStatusEnum = UserStatus.Inactive;
                    user.Created        = DateTime.Now;
                    user.LastAccessed   = DateTime.Now;
                    user.ScreenName     = user.ScreenName ?? user.Email;

                    user = db.Users.Add(user);
                    db.SaveChanges();

                    model = GetUserModelForAdd();

                    status = Status.Success;
                    return(true);
                }
            }
            catch (Exception ex)
            {
                LogHelper.LogFatalError("Manage.UserModelHelper.Add", ex);
                throw ex;
            }
        }
Exemplo n.º 9
0
        public static bool Add(this EventModel model, out Status status)
        {
            try
            {
                using (var db = new DbContextHelper())
                {
                    var entity = db.Events.Create();
                    entity.IdKey = Guid.NewGuid();
                    entity.Title = model.Title;
                    entity.Description = model.Description.Sanitize();
                    entity.Start = Convert.ToDateTime(string.Format("{0} {1}", model.StartDate, model.StartTime));
                    entity.End = Convert.ToDateTime(string.Format("{0} {1}", model.EndDate, model.EndTime));
                    entity.Speakers = model.Speakers;
                    entity.Location = model.Location;
                    entity.Created = DateTime.Now;
                    entity.Approved = model.Approved;
                    
                    var ministries = model.Ministries.GetMinistriesFromModel(db);
                    if (ministries.Count > 0)
                    {
                        entity.Ministries = new List<Ministry>();
                        foreach (var ministry in ministries)
                            ministries.Add(ministry);
                    }

                    entity = db.Events.Add(entity);
                    db.SaveChanges();

                    model.Id = entity.Id;
                    model.IdKey = entity.IdKey;

                    model = GetEventModelForAdd();

                    status = Status.Success;
                    return true;
                }
            }
            catch (Exception ex)
            {
                LogHelper.LogFatalError("ManageEventModelHelper.Add", ex);
                status = Status.SystemException;
            }

            return false;
        }
Exemplo n.º 10
0
        public static bool Add(this ArticleModel model, out Status status)
        {
            try
            {
                using (var db = new DbContextHelper())
                {
                    var entity = db.Articles.Create();
                    entity.Title = model.Title;
                    entity.Description = model.Description.Sanitize();
                    entity.Start = Convert.ToDateTime(model.StartDate);
                    entity.End = Convert.ToDateTime(model.EndDate);
                    entity.Summary =  model.Summary.Sanitize();
                    entity.CategoryId = model.CategoryId;
                    entity.Author = model.Author;
                    entity.Approved = model.Approved;
                    entity.Keywords = model.Keywords;
                    entity.Created = DateTime.Now;

                    var ministries = model.Ministries.GetMinistriesFromModel(db);
                    if (ministries.Count > 0)
                    {
                        entity.Ministries = new List<Ministry>();
                        foreach (var ministry in ministries)
                            entity.Ministries.Add(ministry);
                    }

                    entity = db.Articles.Add(entity);
                    db.SaveChanges();

                    model.Id = entity.Id;
                    model.IdKey = entity.IdKey;

                    status = Status.Success;
                    return true;
                }
            }
            catch (Exception ex)
            {
                LogHelper.LogFatalError("ManageArticleModelHelper.Add", ex);
                status = Status.SystemException;
            }

            return false;
        }
Exemplo n.º 11
0
        public static Quote GetQuote(Guid idKey)
        {
            try
            {
                using (var db = new DbContextHelper())
                {
                    var quote = (from q in db.Quotes
                                 where q.IdKey == idKey
                                 select q).FirstOrDefault();

                    return(quote);
                }
            }
            catch (Exception ex)
            {
                LogHelper.LogFatalError("ManageQuoteModelHelper.GetQuote", ex);
                throw ex;
            }
        }
Exemplo n.º 12
0
        public static Ministry GetMinistry(Guid idKey)
        {
            try
            {
                using (var db = new DbContextHelper())
                {
                    var entity = (from m in db.Ministries.Include("Articles").Include("Events").Include("Users")
                                  where m.IdKey == idKey
                                  select m).FirstOrDefault();

                    return(entity);
                }
            }
            catch (Exception ex)
            {
                LogHelper.LogFatalError("ManageMinistryModelHelper.Get", ex);
                throw ex;
            }
        }
Exemplo n.º 13
0
        public static Article GetArticle(Guid idKey)
        {
            try
            {
                using (var db = new DbContextHelper())
                {
                    var entity = (from m in db.Articles
                                  where m.IdKey == idKey
                                  select m).FirstOrDefault();

                    return(entity);
                }
            }
            catch (Exception ex)
            {
                LogHelper.LogFatalError("ManageArticleModelHelper.Get", ex);
                throw ex;
            }
        }
Exemplo n.º 14
0
        /// <summary>
        /// Authorize the current user on the site. Authenication occurs via Facebook app
        /// </summary>
        /// <param name="model"></param>
        /// <param name="status"></param>
        /// <returns></returns>
        public static bool Authorize(this UserModel model, out Status status)
        {
            try
            {
                using (var db = new DbContextHelper())
                {
                    var user = ManageUserModelHelper.GetUser(db, model.IdKey, model.Email);
                    if (user == null)
                    {
                        status = Status.InvalidLoginPassword;
                        return false;
                    }

                    user.AccessToken = model.AccessToken;
                    user.LastAccessed = DateTime.Now;
                    user.UserStatusEnum = UserStatus.Online;
                    db.SaveChanges();

                    // Save the user basic information
                    model.Id = user.Id;
                    model.IdKey = user.IdKey;
                    model.ScreenName = user.ScreenName;
                    model.FirstName = user.FirstName;
                    model.LastName = user.LastName;
                    model.UserStatus = user.UserStatusEnum;
                    model.Roles = user.Roles.ToList().GetRoleModelsFromRoles();

                    model.CreateAuthorizationTicket();

                    status = Status.Success;
                    return true;
                }
            }

            catch (Exception ex)
            {
                LogHelper.LogFatalError("UserModelHelper.Authorize", ex);
                status = Status.SystemException;
            }

            return false;
        }
Exemplo n.º 15
0
        private static User CreateNewUserFromModel(UserModel model, DbContextHelper db)
        {
            if (model == null)
            {
                return(null);
            }

            User user = db.Users.Create();

            user.FirstName      = model.FirstName;
            user.LastName       = model.LastName;
            user.ScreenName     = model.ScreenName;
            user.Email          = model.Email;
            user.UserStatusEnum = model.UserStatus;
            user.LastAccessed   = model.LastAcecssed;
            user.Created        = model.Created;
            user.IdKey          = model.IdKey;

            var roles = ManageRoleModelHelper.GetRolesFromModel(model.Roles, db);

            if (roles.Count > 0)
            {
                user.Roles = new List <Role>();
                foreach (var role in roles)
                {
                    user.Roles.Add(role);
                }
            }

            var ministries = ManageMinistryModelHelper.GetMinistriesFromModel(model.Ministries, db);

            if (ministries.Count > 0)
            {
                user.Ministries = new List <Ministry>();
                foreach (var ministry in ministries)
                {
                    user.Ministries.Add(ministry);
                }
            }

            return(user);
        }
Exemplo n.º 16
0
        /// <summary>
        /// Creates a paginated entity model used for the EventsController.Index view
        /// </summary>
        /// <param name="model">Paginated model of events</param>
        /// <param name="pageNumber">The page number to return</param>
        /// <returns></returns>
        public static void GetPaginatedEvents(this PaginationModel<EventModel> model, int pageNumber = 1)
        {
            try
            {
                using (var db = new DbContextHelper())
                {
                    var endDate = DateTime.Now.EndOfDay();
                    var data = (from e in db.Events 
                                where e.End > endDate select e)
                                .OrderBy(model.SortOptions.Column, model.SortOptions.Direction)
                                .ToList();

                    List<EventModel> models = new List<EventModel>();
                    foreach (var e in data)
                    {
                        models.Add(new EventModel
                                  {
                                      Id = e.Id,
                                      IdKey = e.IdKey,
                                      Title = e.Title,
                                      Description = e.Description,
                                      Start = e.Start,
                                      End = e.End,
                                      StartDate = e.Start.ToShortDateString(),
                                      EndDate = e.End.ToShortDateString(),
                                      StartTime = e.Start.ToString("hh:mm tt"),
                                      EndTime = e.End.ToString("hh:mm tt"),
                                      Speakers = e.Speakers,
                                      Location = e.Location,
                                      Approved = e.Approved,
                                  });
                    }

                    model.Data = new CustomPagination<EventModel>(models, pageNumber, 20, models.Count);
                }                
            }
            catch (Exception ex)
            {
                LogHelper.LogFatalError("ManageEventModelHelper.GetPaginatedEvents", ex);
                throw ex;
            }
        }
Exemplo n.º 17
0
        /// <summary>
        /// Creates a paginated entity model used for the EventsController.Index view
        /// </summary>
        /// <param name="model">Paginated model of events</param>
        /// <param name="pageNumber">The page number to return</param>
        /// <returns></returns>
        public static void GetPaginatedEvents(this PaginationModel <EventModel> model, int pageNumber = 1)
        {
            try
            {
                using (var db = new DbContextHelper())
                {
                    var endDate = DateTime.Now.EndOfDay();
                    var data    = (from e in db.Events
                                   where e.End > endDate select e)
                                  .OrderBy(model.SortOptions.Column, model.SortOptions.Direction)
                                  .ToList();

                    List <EventModel> models = new List <EventModel>();
                    foreach (var e in data)
                    {
                        models.Add(new EventModel
                        {
                            Id          = e.Id,
                            IdKey       = e.IdKey,
                            Title       = e.Title,
                            Description = e.Description,
                            Start       = e.Start,
                            End         = e.End,
                            StartDate   = e.Start.ToShortDateString(),
                            EndDate     = e.End.ToShortDateString(),
                            StartTime   = e.Start.ToString("hh:mm tt"),
                            EndTime     = e.End.ToString("hh:mm tt"),
                            Speakers    = e.Speakers,
                            Location    = e.Location,
                            Approved    = e.Approved,
                        });
                    }

                    model.Data = new CustomPagination <EventModel>(models, pageNumber, 20, models.Count);
                }
            }
            catch (Exception ex)
            {
                LogHelper.LogFatalError("ManageEventModelHelper.GetPaginatedEvents", ex);
                throw ex;
            }
        }
Exemplo n.º 18
0
        /// <summary>
        /// Gets the user from the application database
        /// </summary>
        /// <param name="db">Database context helper (required)</param>
        /// <param name="idKey">System generated identification key (required)</param>
        /// <param name="email">Email address (Facebook me.email)</param>
        /// <param name="screenName">Screen name (Facebook me.username)</param>
        /// <param name="accessToken">Access Token (Facebook Login Dialog and Open Graph API OAuth)</param>
        /// <param name="facebookId">Numeric identification code (Facebook me.id)</param>
        /// <returns></returns>
        public static User GetUser(DbContextHelper db, Guid idKey, string email = "", string screenName = "", string accessToken = "", int facebookId = -1)
        {
            User user = null;

            try
            {
                // Note: order is important on the Include clause
                // If token is set the user status will be for example ResetPassword, etc
                user = (from u in db.Users.Include("Ministries").Include(ROLES)
                        where u.IdKey == idKey || u.Email.Equals(email) || u.ScreenName.Equals(screenName) || u.AccessToken.Equals(accessToken)
                        select u).FirstOrDefault();
            }
            catch (Exception ex)
            {
                LogHelper.LogFatalError("UserModelHelper.GetUser", ex);
                throw ex;
            }

            return(user);
        }
Exemplo n.º 19
0
        /// <summary>
        /// Determines if the screen name exists in the system
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static bool ScreenNameExists(this UserModel model)
        {
            bool result = false;

            try
            {
                using (var db = new DbContextHelper())
                {
                    result = db.Users
                             .Where(u => u.ScreenName.Equals(model.ScreenName) && u.IdKey != model.IdKey)
                             .Select(u => true)
                             .FirstOrDefault();
                }
            }
            catch (Exception ex)
            {
                LogHelper.LogFatalError("UserModelHelper.ScreenNameExists", ex);
                throw ex;
            }
            return(result);
        }
Exemplo n.º 20
0
        /// <summary>
        /// Gets a list of select models
        /// </summary>
        /// <param name="items"></param>
        /// <param name="db"></param>
        /// <returns></returns>
        public static List<Role> GetRoles(this List<SelectListItem> items, DbContextHelper db)
        {
            List<Role> roles = new List<Role>();

            try
            {
                var idKeys = items.Where(i => i.Selected == true).Select(i => new Guid(i.Value)).ToList();
                roles = (from r in db.Roles
                         where idKeys.Contains(r.IdKey)
                         select r).ToList();

                // Always return at least basic item
                if (roles.Count == 0)
                    roles.Add(GetRole(Role.BASIC_ID, db));

                return roles;
            }
            catch (Exception ex)
            {
                LogHelper.LogFatalError("ManageRoleModelHelper.GetRoles", ex);
                throw ex;
            }
        }
Exemplo n.º 21
0
        public static bool Update(this QuoteModel model, out Status status)
        {
            try
            {
                using (var db = new DbContextHelper())
                {
                    var entity = (from q in db.Quotes
                                  where q.Id == model.Id || q.IdKey == model.IdKey
                                  select q).FirstOrDefault();

                    if (entity == null)
                    {
                        status = Status.DataNotFound;
                        return(false);
                    }

                    entity.Description = model.Description.Sanitize();
                    entity.Source      = model.Source;
                    entity.Modified    = DateTime.Now;
                    entity.Approved    = model.Approved;

                    db.SaveChanges();

                    model  = GetQuoteModelForEdit(entity.IdKey.ToString());
                    status = Status.Success;
                    return(true);
                }
            }
            catch (Exception ex)
            {
                LogHelper.LogFatalError("ManageQuoteModelHelper.Add", ex);
                status = Status.SystemException;
            }

            return(false);
        }
Exemplo n.º 22
0
        /// <summary>
        /// Gets a paginated listed of system users
        /// </summary>
        /// <param name="model">Paginated model for managed users</param>
        /// <param name="pageNumber">the page number to return</param>
        /// <returns></returns>
        public static void GetPaginatedUsers(this PaginationModel<UserModel> model, int pageNumber = 1)
        {
            try
            {
                using(var db = new DbContextHelper())
                {
                    var data = (from u in db.Users
                                select new UserModel
                                {
                                    Id = u.Id,
                                    IdKey = u.IdKey,
                                    FirstName = u.FirstName,
                                    LastName = u.LastName,
                                    ScreenName = u.ScreenName,
                                    Email = u.Email,
                                    UserStatus = (UserStatus)u.UserStatus,
                                })
                                .OrderBy(model.SortOptions.Column, model.SortOptions.Direction)
                                .ToList();

                    model.Data = new CustomPagination<UserModel>(data, pageNumber, 20, data.Count);                                 
                }
            }
            catch (Exception ex)
            {
                LogHelper.LogFatalError("UserModelHelper.GetPaginatedUsers", ex);
                throw ex;
            }
        }
Exemplo n.º 23
0
 /// <summary>
 /// Determines if the screen name exists in the system
 /// </summary>
 /// <param name="model"></param>
 /// <returns></returns>
 public static bool ScreenNameExists(this UserModel model)
 {
     bool result = false;
     try
     {
         using (var db = new DbContextHelper())
         {
             result = db.Users
                 .Where(u => u.ScreenName.Equals(model.ScreenName) && u.IdKey != model.IdKey)
                     .Select(u => true)
                     .FirstOrDefault();
         }
     }
     catch (Exception ex)
     {
         LogHelper.LogFatalError("UserModelHelper.ScreenNameExists", ex);
         throw ex;
     }
     return result;
 }
Exemplo n.º 24
0
        public static RoleModel GetRoleModel(string guid)
        {
            try
            {
                Guid idKey = Guid.Empty;
                if (!Guid.TryParse(guid, out idKey))
                    idKey = Guid.Empty;

                using (var db = new DbContextHelper())
                {
                    var model = (from r in db.Roles
                                 where r.IdKey == idKey || r.Id == Role.BASIC_ID //include basic 
                                 orderby r.Id descending
                                 select new RoleModel
                                 {
                                     Id = r.Id,
                                     IdKey = r.IdKey,
                                     Name = r.Name,
                                     Description = r.Description,
                                 }).FirstOrDefault();

                    return model;
                }
            }
            catch (Exception ex)
            {
                LogHelper.LogFatalError("ManageRoleModelHelper.GetRoleModel", ex);
                throw ex;
            }
        }
Exemplo n.º 25
0
        public static MinistryModel GetMinistryModel(string guid)
        {
            try
            {
                Guid idKey = Guid.Empty;
                if (!Guid.TryParse(guid, out idKey))
                    idKey = Guid.Empty;

                using (var db = new DbContextHelper())
                {
                    var model = (from m in db.Ministries
                                 where m.IdKey == idKey
                                 orderby m.Id descending
                                 select new MinistryModel
                                 {
                                     Id = m.Id,
                                     IdKey = m.IdKey,
                                     Name = m.Name,
                                     Description = m.Description,
                                 }).FirstOrDefault();

                    return model;
                }
            }
            catch (Exception ex)
            {
                LogHelper.LogFatalError("ManageMinistryModelHelper.GetMinistryModel", ex);
                throw ex;
            }
        }
Exemplo n.º 26
0
        private static User CreateNewUserFromModel(UserModel model, DbContextHelper db)
        {
            if (model == null)
                return null;

            User user = db.Users.Create();

            user.FirstName = model.FirstName;
            user.LastName = model.LastName;
            user.ScreenName = model.ScreenName;
            user.Email = model.Email;
            user.UserStatusEnum = model.UserStatus;
            user.LastAccessed = model.LastAcecssed;
            user.Created = model.Created;
            user.IdKey = model.IdKey;

            var roles = ManageRoleModelHelper.GetRolesFromModel(model.Roles, db);
            if (roles.Count > 0)
            {
                user.Roles = new List<Role>();
                foreach (var role in roles)
                    user.Roles.Add(role);
            }

            var ministries = ManageMinistryModelHelper.GetMinistriesFromModel(model.Ministries, db);
            if (ministries.Count > 0)
            {
                user.Ministries = new List<Ministry>();
                foreach (var ministry in ministries)
                    user.Ministries.Add(ministry);
            }

            return user;
        }
Exemplo n.º 27
0
        public static bool Add(this UserModel model, out Status status)
        {
            try
            {
                if (model.Roles.Count == 0)
                {
                    status = Status.RoleRequired;
                    return false;
                }

                if (model.EmailExists())
                {
                    status = Status.DuplicateEmail;
                    return false;
                }

                if (model.ScreenNameExists())
                {
                    status = Status.DuplicateScreenName;
                    return false;
                }
                
                using (var db = new DbContextHelper())
                {
                    var user = CreateNewUserFromModel(model, db);                    
                    user.UserStatusEnum = UserStatus.Inactive;
                    user.Created = DateTime.Now;
                    user.LastAccessed = DateTime.Now;
                    user.ScreenName = user.ScreenName ?? user.Email;                    
                    
                    user = db.Users.Add(user);
                    db.SaveChanges();

                    model = GetUserModelForAdd();

                    status = Status.Success;
                    return true;
                }
            }
            catch (Exception ex)
            {
                LogHelper.LogFatalError("Manage.UserModelHelper.Add", ex);
                throw ex;
            }
        }
Exemplo n.º 28
0
        public static Ministry GetMinistry(Guid idKey)
        {
            
            try
            {
                using(var db = new DbContextHelper())
                {
                    var entity = (from m in db.Ministries.Include("Articles").Include("Events").Include("Users")
                              where m.IdKey == idKey
                              select m).FirstOrDefault();

                    return entity;
                }
            }
            catch (Exception ex)
            {
                LogHelper.LogFatalError("ManageMinistryModelHelper.Get", ex);
                throw ex;
            }
        }
Exemplo n.º 29
0
        public static Event GetEvent(Guid idKey)
        {

            try
            {
                using (var db = new DbContextHelper())
                {
                    var entity = (from m in db.Events
                                  where m.IdKey == idKey
                                  select m).FirstOrDefault();

                    return entity;
                }
            }
            catch (Exception ex)
            {
                LogHelper.LogFatalError("ManageEventModelHelper.Get", ex);
                throw ex;
            }
        }
Exemplo n.º 30
0
        public static bool Delete(Guid idKey, out Status status)
        {
            try
            {
                
                using (var db = new DbContextHelper())
                {
                    var entity = (from m in db.Events
                                  where m.IdKey == idKey
                                  select m).FirstOrDefault();

                    if (entity == null)
                    {
                        status = Status.DataNotFound;
                        return false;
                    }

                    entity = db.Events.Remove(entity);
                    db.SaveChanges();

                    status = Status.Success;
                    return true;
                }
            }
            catch (Exception ex)
            {
                LogHelper.LogFatalError("ManageEventModelHelper.Delete", ex);
                status = Status.SystemException;
            }

            return false;
        }
Exemplo n.º 31
0
        public static bool Update(this EventModel model, out Status status)
        {
            try
            {
                using (var db = new DbContextHelper())
                {
                    var entity = (from m in db.Events
                                  where m.IdKey == model.IdKey
                                  select m).FirstOrDefault();

                    if (entity == null)
                    {
                        status = Status.DataNotFound;
                        return false;
                    }

                    entity.Title = model.Title;
                    entity.Description = model.Description.Sanitize();
                    entity.Start = Convert.ToDateTime(string.Format("{0} {1}", model.StartDate, model.StartTime));
                    entity.End = Convert.ToDateTime(string.Format("{0} {1}", model.EndDate, model.EndTime));
                    entity.Speakers = model.Speakers;
                    entity.Location = model.Location;
                    entity.Modified = DateTime.Now;
                    entity.Approved = model.Approved;

                    if (entity.Ministries == null) entity.Ministries = new List<Ministry>();
                    var ministries = model.Ministries.GetMinistriesFromModel(db);
                    if (ministries.Count > 0)
                    {
                        entity.Ministries.Clear();
                        foreach (var ministry in ministries)
                            ministries.Add(ministry);
                    }

                    db.SaveChanges();

                    status = Status.Success;
                    return true;
                }
            }
            catch (Exception ex)
            {
                LogHelper.LogFatalError("ManageEventModelHelper.Update", ex);
                status = Status.SystemException;
            }

            return false;
        }
Exemplo n.º 32
0
        public static List<MinistryModel> GetAllMinistries()
        {
            List<MinistryModel> models = new List<MinistryModel>();
            try
            {
                using (var db = new DbContextHelper())
                {
                    models = (from m in db.Ministries
                              orderby m.Name
                              select new MinistryModel
                              {
                                  Id = m.Id,
                                  IdKey = m.IdKey,
                                  Name = m.Name,
                                  Description = m.Description,
                              }).ToList();

                    return models;
                }
            }
            catch (Exception ex)
            {
                LogHelper.LogFatalError("ManageMinistryModelHelper.GetAllMinistries", ex);
                throw ex;
            }
        }
Exemplo n.º 33
0
        /// <summary>
        /// Gets the user from the application database
        /// </summary>
        /// <param name="db">Database context helper (required)</param>
        /// <param name="idKey">System generated identification key (required)</param>
        /// <param name="email">Email address (Facebook me.email)</param>
        /// <param name="screenName">Screen name (Facebook me.username)</param>
        /// <param name="accessToken">Access Token (Facebook Login Dialog and Open Graph API OAuth)</param>
        /// <param name="facebookId">Numeric identification code (Facebook me.id)</param>
        /// <returns></returns>
        public static User GetUser(DbContextHelper db, Guid idKey, string email = "", string screenName ="", string accessToken = "", int facebookId = -1)
        {
            User user = null;
            try
            {
                // Note: order is important on the Include clause
                // If token is set the user status will be for example ResetPassword, etc
                user = (from u in db.Users.Include("Ministries").Include(ROLES)
                        where u.IdKey == idKey || u.Email.Equals(email) || u.ScreenName.Equals(screenName) || u.AccessToken.Equals(accessToken)
                        select u).FirstOrDefault();
            }
            catch (Exception ex)
            {
                LogHelper.LogFatalError("UserModelHelper.GetUser", ex);
                throw ex;
            }

            return user;
        }
Exemplo n.º 34
0
 /// <summary>
 /// Gets the application user
 /// </summary>
 /// <param name="idKey">System generated identification key</param>
 /// <returns></returns>
 public static UserModel Get(Guid idKey)
 {
     try
     {
         using (var db = new DbContextHelper())
         {
             var user = GetUser(db, idKey);
             return GetModelFromUser(user);
         }
     }
     catch (Exception ex)
     {
         LogHelper.LogFatalError("UserModelHelper.Get", ex);
         throw ex;
     }
 }
Exemplo n.º 35
0
        public static bool Update(this UserModel model, out Status status)
        {
            try
            {
                if (model.EmailExists())
                {
                    status = Status.DuplicateEmail;
                    return(false);
                }

                if (model.ScreenNameExists())
                {
                    status = Status.DuplicateScreenName;
                    return(false);
                }

                using (var db = new DbContextHelper())
                {
                    User entity = GetUser(db, model.IdKey);

                    if (entity == null)
                    {
                        status = Status.DataNotFound;
                        return(false);
                    }

                    entity.FirstName  = model.FirstName;
                    entity.LastName   = model.LastName;
                    entity.ScreenName = model.ScreenName;
                    entity.Modified   = DateTime.Now;

                    entity.AccessToken = model.AccessToken;
                    entity.FacebookId  = model.FacebookId;
                    entity.Link        = model.Link;

                    var currentTicket = GetFormsAuthenticationTicket();
                    if (currentTicket.IsInRole("Administrator"))
                    {
                        if (model.Roles.Count == 0)
                        {
                            status = Status.RoleRequired;
                            return(false);
                        }


                        // Update the entity user/item relationship
                        entity.Roles.Clear();
                        var roles = model.Roles.GetRolesFromModel(db);
                        foreach (var item in roles)
                        {
                            entity.Roles.Add(item);
                        }

                        entity.Ministries.Clear();
                        // Update the entity user/ministry relationship
                        if (model.Ministries != null && model.Ministries.Count > 0)
                        {
                            var ministries = model.Ministries.GetMinistriesFromModel(db);
                            foreach (var item in ministries)
                            {
                                entity.Ministries.Add(item);
                            }
                        }
                    } //end IsInRole check

                    db.SaveChanges();

                    try // Re-issue authorization currentTicket
                    {
                        // Get current user id
                        Guid userIdKey = currentTicket.IdKey();

                        // Match?
                        if (userIdKey == entity.IdKey)
                        {
                            model.CreateAuthorizationTicket();
                        }
                    }
                    catch (Exception ex)
                    {
                        LogHelper.LogFatalError("UserModelHelper.Update", ex);
                    }

                    model = GetUserModelForEdit(entity.IdKey.ToString());

                    status = Status.Success;
                    return(true);
                }
            }
            catch (Exception ex)
            {
                LogHelper.LogFatalError("Manage.UserModelHelper.Edit", ex);
                status = Status.SystemException;
            }

            return(false);
        }
Exemplo n.º 36
0
        public static User GetUserFromModel(UserModel model, DbContextHelper db)
        {
            if (model == null)
                return null;

            User user = new User
            {
                FirstName = model.FirstName,
                LastName = model.LastName,
                ScreenName = model.ScreenName,
                Email = model.Email,
                Link = model.Link,
                AccessToken = model.AccessToken,
                FacebookId = model.FacebookId,                
                UserStatusEnum = model.UserStatus,
                LastAccessed = model.LastAcecssed,
                Created = model.Created,
                Id = model.Id,
                Roles = ManageRoleModelHelper.GetRolesFromModel(model.Roles, db)
            };

            return user;
        }
Exemplo n.º 37
0
        public static List<Ministry> GetMinistriesFromModel(this List<MinistryModel> models, DbContextHelper db)
        {
            List<Ministry> ministries = new List<Ministry>();

            try
            {
                var idKeys = models.Select(m => m.IdKey).ToList();
                ministries = (from m in db.Ministries
                         where idKeys.Contains(m.IdKey)
                         select m).ToList();

                return ministries;
            }
            catch (Exception ex)
            {
                LogHelper.LogFatalError("ManageMinistryModelHelper.GetMinistriesFromModel", ex);
                throw ex;
            }
        }
Exemplo n.º 38
0
        public static bool Update(this UserModel model, out Status status)
        {
            try
            {                
                if (model.EmailExists())
                {
                    status = Status.DuplicateEmail;
                    return false;
                }

                if (model.ScreenNameExists())
                {
                    status = Status.DuplicateScreenName;
                    return false;
                }
                
                using (var db = new DbContextHelper())
                {
                    User entity = GetUser(db, model.IdKey);

                    if (entity == null)
                    {
                        status = Status.DataNotFound;
                        return false;
                    }

                    entity.FirstName = model.FirstName;
                    entity.LastName = model.LastName;
                    entity.ScreenName = model.ScreenName;
                    entity.Modified = DateTime.Now;

                    entity.AccessToken = model.AccessToken;
                    entity.FacebookId = model.FacebookId;
                    entity.Link = model.Link;

                    var currentTicket = GetFormsAuthenticationTicket();
                    if (currentTicket.IsInRole("Administrator"))
                    {
                        if (model.Roles.Count == 0)
                        {
                            status = Status.RoleRequired;
                            return false;
                        }


                        // Update the entity user/item relationship
                        entity.Roles.Clear();
                        var roles = model.Roles.GetRolesFromModel(db);
                        foreach (var item in roles)
                        {
                            entity.Roles.Add(item);
                        }

                        entity.Ministries.Clear();
                        // Update the entity user/ministry relationship
                        if (model.Ministries != null && model.Ministries.Count > 0)
                        {
                            var ministries = model.Ministries.GetMinistriesFromModel(db);
                            foreach (var item in ministries)
                            {
                                entity.Ministries.Add(item);
                            }
                        }
                    } //end IsInRole check

                    db.SaveChanges();
                    
                    try // Re-issue authorization currentTicket
                    { 
                        // Get current user id
                        Guid userIdKey = currentTicket.IdKey();

                        // Match?
                        if (userIdKey == entity.IdKey)
                            model.CreateAuthorizationTicket();
                    }
                    catch (Exception ex)
                    {
                        LogHelper.LogFatalError("UserModelHelper.Update", ex);
                    }

                    model = GetUserModelForEdit(entity.IdKey.ToString());

                    status = Status.Success;
                    return true;
                }
            }
            catch (Exception ex)
            {
                LogHelper.LogFatalError("Manage.UserModelHelper.Edit", ex);
                status = Status.SystemException;
            }

            return false;
        }        
Exemplo n.º 39
0
        public static List<Role> GetRolesFromModel(this List<RoleModel> models, DbContextHelper db)
        {
            List<Role> roles = new List<Role>();

            try
            {
                var idKeys = models.Select(m => m.IdKey).ToList();
                roles = (from r in db.Roles
                         where idKeys.Contains(r.IdKey)
                         select r).ToList();

                // Always return at least basic item
                if (roles.Count == 0)
                    roles.Add(GetRole(Role.BASIC_ID, db));

                return roles;
            }
            catch (Exception ex)
            {
                LogHelper.LogFatalError("ManageRoleModelHelper.GetRolesFromModel", ex);
                throw ex;
            }
        }
Exemplo n.º 40
0
 public static List<RoleModel> GetAllRoles()
 {
     List<RoleModel> roles = new List<RoleModel>();
     try
     {
         using(var db = new DbContextHelper())
         {
             roles = (from r in db.Roles
                      orderby r.Name
                      select new RoleModel
                      {
                          Id = r.Id,
                          IdKey = r.IdKey,
                          Name = r.Name,
                          Description = r.Description
                      }).ToList();
         }
     }
     catch (Exception ex)
     {
         LogHelper.LogFatalError("Manage.RoleModelHelper.GetAllMinistries", ex);
     }
     return roles;
 }
Exemplo n.º 41
0
        public static List <Ministry> GetMinistriesFromModel(this List <MinistryModel> models, DbContextHelper db)
        {
            List <Ministry> ministries = new List <Ministry>();

            try
            {
                var idKeys = models.Select(m => m.IdKey).ToList();
                ministries = (from m in db.Ministries
                              where idKeys.Contains(m.IdKey)
                              select m).ToList();

                return(ministries);
            }
            catch (Exception ex)
            {
                LogHelper.LogFatalError("ManageMinistryModelHelper.GetMinistriesFromModel", ex);
                throw ex;
            }
        }