PageRenderTime 92ms CodeModel.GetById 78ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 1ms

/BlogEngine/BlogEngine.NET/App_Code/Profile.cs

#
C# | 157 lines | 111 code | 28 blank | 18 comment | 18 complexity | 3a80c6594c1722670bfa498397e9d31c MD5 | raw file
  1namespace App_Code
  2{
  3    using System;
  4    using System.Web.Script.Services;
  5    using System.Web.Security;
  6    using System.Web.Services;
  7
  8    using BlogEngine.Core;
  9    using BlogEngine.Core.Json;
 10
 11    /// <summary>
 12    /// The profile.
 13    /// </summary>
 14    [WebService(Namespace = "http://tempuri.org/")]
 15    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
 16    [ScriptService]
 17    public class Profile : WebService
 18    {
 19        #region Constants and Fields
 20
 21        /// <summary>
 22        ///     JSON object that will be return back to client
 23        /// </summary>
 24        private readonly JsonResponse response;
 25
 26        #endregion
 27
 28        #region Constructors and Destructors
 29
 30        /// <summary>
 31        /// Initializes a new instance of the <see cref="Profile"/> class.
 32        /// </summary>
 33        public Profile()
 34        {
 35            this.response = new JsonResponse();
 36        }
 37
 38        #endregion
 39
 40        #region Public Methods
 41
 42        /// <summary>
 43        /// Saves the specified id.
 44        /// </summary>
 45        /// <param name="id">The profile id.</param>
 46        /// <param name="vals">The values.</param>
 47        /// <param name="roles">The roles.</param>
 48        /// <returns>JSON response.</returns>
 49        [WebMethod]
 50        public JsonResponse Save(string id, string[] vals, string[] roles)
 51        {
 52            this.response.Success = false;
 53
 54            if (string.IsNullOrEmpty(vals[0]))
 55            {
 56                this.response.Message = Resources.labels.displayNameIsRequired;
 57                return this.response;
 58            }
 59
 60            bool isSelf = id.Equals(Security.CurrentUser.Identity.Name, StringComparison.OrdinalIgnoreCase);
 61
 62            if (isSelf && !Security.IsAuthorizedTo(Rights.EditOwnUser))
 63            {
 64                this.response.Message = Resources.labels.notAuthorized;
 65                return this.response;
 66            }
 67            else if (!isSelf && !Security.IsAuthorizedTo(Rights.EditOtherUsers))
 68            {
 69                this.response.Message = Resources.labels.notAuthorized;
 70                return this.response;
 71            }
 72
 73            var pf = AuthorProfile.GetProfile(id) ?? new AuthorProfile(id);
 74
 75            try
 76            {
 77                
 78                pf.DisplayName = vals[0];
 79                pf.FirstName = vals[1];
 80                pf.MiddleName = vals[2];
 81                pf.LastName = vals[3];
 82                pf.EmailAddress = vals[4];
 83
 84                DateTime date;
 85                if (vals[5].Length == 0)
 86                {
 87                    vals[5] = "1/1/1001";
 88                }
 89
 90                if (DateTime.TryParse(vals[5], out date))
 91                {
 92                    pf.Birthday = date;
 93                }
 94                else
 95                {
 96                    this.response.Message = "Date must be in format mm/dd/yyyy";
 97                    return this.response;
 98                }
 99
100                pf.PhotoUrl = vals[6];
101                pf.Private = false;
102
103                bool prv;
104                if (bool.TryParse(vals[7], out prv))
105                {
106                    pf.Private = prv;
107                }
108
109                pf.PhoneMobile = vals[8];
110                pf.PhoneMain = vals[9];
111                pf.PhoneFax = vals[10];
112
113                pf.CityTown = vals[11];
114                pf.RegionState = vals[12];
115                pf.Country = vals[13]; // ddlCountry.SelectedValue;
116
117                // pf.Company = tbCompany.Text;
118                pf.AboutMe = vals[14];
119
120                pf.Save();
121
122                bool saveRoles = false;
123                if (isSelf && Security.IsAuthorizedTo(Rights.EditOwnRoles))
124                    saveRoles = true;
125                else if (!isSelf && Security.IsAuthorizedTo(Rights.EditOtherUsersRoles))
126                    saveRoles = true;
127
128                if (saveRoles)
129                {
130                    // remove all user roles and add only checked
131                    string[] currentRoles = Roles.GetRolesForUser(id);
132                    if (currentRoles.Length > 0)
133                    {
134                        Roles.RemoveUserFromRoles(id, currentRoles);
135                    }
136                    
137                    if (roles.GetLength(0) > 0)
138                    {
139                        Roles.AddUsersToRoles(new string[] { id }, roles);
140                    }
141                }
142            }
143            catch (Exception ex)
144            {
145                Utils.Log(string.Format("Profile.Edit: {0}", ex.Message));
146                this.response.Message = string.Format(Resources.labels.couldNotUpdateProfile, vals[0]);
147                return this.response;
148            }
149
150            this.response.Success = true;
151            this.response.Message = string.Format(Resources.labels.profileUpdated, vals[0]);
152            return this.response;
153        }
154
155        #endregion
156    }
157}