Esempio n. 1
0
        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);
        }
Esempio n. 2
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;
     }
 }
        public static bool Add(this QuoteModel model, out Status status)
        {
            try
            {
                using (var db = new DbContextHelper())
                {
                    var entity = db.Quotes.Create();
                    entity.IdKey = Guid.NewGuid();
                    entity.Description = model.Description.Sanitize();
                    entity.Source = model.Source;
                    entity.Created = DateTime.Now;
                    entity.Approved = model.Approved;

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

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

                    model = GetQuoteModelForAdd();

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

            return false;
        }
Esempio n. 4
0
        public static bool Delete(Guid idKey, out Status status)
        {
            try
            {
                using (var db = new DbContextHelper())
                {
                    var entity = (from m in db.Articles
                                  where m.IdKey == idKey
                                  select m).FirstOrDefault();

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

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

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

            return(false);
        }
Esempio n. 5
0
        public static bool Update(this MinistryModel model, out Status status)
        {
            try
            {
                using (var db = new DbContextHelper())
                {
                    var entity = (from m in db.Ministries
                                  where m.IdKey == model.IdKey
                                  select m).FirstOrDefault();

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

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

                    db.SaveChanges();

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

            return(false);
        }
Esempio n. 6
0
        public static bool Update(this ArticleModel model, out Status status)
        {
            try
            {
                using (var db = new DbContextHelper())
                {
                    var entity = (from m in db.Articles
                                  where m.Id == model.Id && 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(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.Modified    = DateTime.Now;
                    entity.Keywords    = model.Keywords;

                    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)
                        {
                            entity.Ministries.Add(ministry);
                        }
                    }

                    db.SaveChanges();

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

            return(false);
        }
        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);
        }
        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);
        }
Esempio n. 9
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);
        }
Esempio n. 10
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;
            }
        }
Esempio n. 11
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;
        }
Esempio n. 12
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);
        }
Esempio n. 13
0
        /// <summary>
        /// Authorize the current user on the site. Authenication occurs via Facebook app (signin)
        /// </summary>
        /// <param name="model">User model</param>
        /// <param name="status">status of authorizing the user</param>
        /// <param name="code">Facebook Oauth code used to retrieve a Facebook access token</param>
        /// <returns></returns>
        /// <remarks>Requires the code to be set on the model. This is used to retrieve a Facebook access token</remarks>
        public static bool Authorize(this UserModel model, string code, out Status status)
        {
            try
            {
                using (var db = new DbContextHelper())
                {
                    // TODO: Validate the access token

                    var user = ManageUserModelHelper.GetUser(db, idKey: model.IdKey, email: model.Email, screenName: model.ScreenName, facebookId: model.FacebookId);
                    if (user == null)
                    {
                        return model.Register(out status);
                    }

                    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("FacebookHelper.Authorize", ex);
                status = Status.SystemException;
            }

            return false;
        }
Esempio n. 14
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;
        }
Esempio n. 15
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;
        }        
Esempio n. 16
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;
            }
        }
Esempio n. 17
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;
        }
Esempio n. 18
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;
        }
Esempio n. 19
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;
        }
        public static bool Update(this MinistryModel model, out Status status)
        {
            try
            {
                using (var db = new DbContextHelper())
                {
                    var entity = (from m in db.Ministries
                                    where m.IdKey == model.IdKey
                                    select m).FirstOrDefault();

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

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

                    db.SaveChanges();

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

            return false;
        }
Esempio n. 21
0
        /// <summary>
        /// Registers a new user on our application using the information
        /// </summary>
        /// <param name="model">User with the Facebook access token set to a valid value.</param>
        /// <param name="status">Status of registring this user</param>
        /// <returns></returns>
        /// <remarks>Users is not on our system yet. The model's Facebook access token must be set in order to register the user on our application.</remarks>
        public static bool Register(this UserModel model, out Status status)
        {
            try
            {
                using (var db = new DbContextHelper())
                {
                    if (model.EmailExists())
                    {
                        status = Status.DuplicateEmail;
                        return false;
                    }

                    var user = db.Users.Create();
                    user.Roles = new List<Role>();

                    // TODO: Get Facebook app role for current user
                    //       If administrator or developer grant full access
                    if (false)
                    {
                        user.Roles.Add(ManageRoleModelHelper.GetRole(Role.ADMINISTRATOR_ID, db));
                        user.Roles.Add(ManageRoleModelHelper.GetRole(Role.CONTENT_APPROVER_ID, db));
                        user.Roles.Add(ManageRoleModelHelper.GetRole(Role.CONTENT_PUBLISHER_ID, db));
                        user.Roles.Add(ManageRoleModelHelper.GetRole(Role.MINISTRY, db));
                    }
                    else
                    {
                        user.Roles.Add(ManageRoleModelHelper.GetRole(Role.BASIC_ID, db));
                    }

                    user.IdKey = Guid.NewGuid();
                    user.FacebookId = model.FacebookId;
                    user.Link = model.Link;
                    user.FirstName = model.FirstName;
                    user.LastName = model.LastName;
                    user.ScreenName = model.ScreenName;
                    user.AccessToken = model.AccessToken;
                    user.UserStatusEnum = UserStatus.Active;
                    user.Created = DateTime.Now;
                    user.LastAccessed = DateTime.Now;
                    user.Email = model.Email;
                    user.ScreenName = model.Email;

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

                    model.Id = user.Id;
                    model.IdKey = user.IdKey;
                    model.UserIdKey = user.IdKey;

                    List<Role> roles = user.Roles as List<Role>;
                    model.Roles = roles.GetRoleModelsFromRoles();

                    model.CreateAuthorizationTicket();

                    status = Status.Success;
                    return true;
                }
            }
            catch (Exception ex)
            {
                LogHelper.LogFatalError("FacebookHelper.Register", ex);
                status = Status.SystemException;
            }
            return false;
        }
Esempio n. 22
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);
        }
        public static bool Update(this ArticleModel model, out Status status)
        {
            try
            {
                using (var db = new DbContextHelper())
                {
                    var entity = (from m in db.Articles
                                  where m.Id == model.Id && 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(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.Modified = DateTime.Now;
                    entity.Keywords = model.Keywords;

                    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)
                            entity.Ministries.Add(ministry);
                    }

                    db.SaveChanges();

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

            return false;
        }
        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;
        }