Esempio n. 1
0
        public string AddUser(UserInfo info, ResourceInfo resourceInfo)
        {
            try
            {
                if (!String.IsNullOrWhiteSpace(resourceInfo.DataUrl))
                    resourceInfo.Data = JsonWebToken.Base64UrlDecode(resourceInfo.DataUrl);

                string reqHeader = HttpContext.Current.Request.Headers[CodeHelper.HeaderAccessKey];
                if (!String.IsNullOrEmpty(reqHeader))
                {
                    // If token is valid then add user
                    if (TokenAuthorization.CheckBasicAuthorization(reqHeader))
                    {
                        UserManager mgr = new UserManager();
                        // gets territory id by country code
                        TerritoryManager trMgr = new TerritoryManager();
                        info.TerritoryId = trMgr.GetTerritoryIdByCountryCode(info.CountryCode, trMgr.GetTerritoryList()).Id;
                        if (!String.IsNullOrWhiteSpace(info.UserName) && !String.IsNullOrWhiteSpace(info.DisplayName) && !String.IsNullOrWhiteSpace(info.OwnNumber)
                            && !String.IsNullOrWhiteSpace(info.Status) && !String.IsNullOrWhiteSpace(info.SerialNum) && !String.IsNullOrWhiteSpace(info.ComId))
                            return mgr.AddUser(info, resourceInfo);
                        throw new Exception(CodeHelper.UnableToAddUser);
                    }

                    // throw exception stating token is invalid
                    throw new Exception(CodeHelper.InvalidToken);
                }

                throw new Exception(CodeHelper.InvalidHeader);
            }
            catch (Exception ex)
            {
                //HttpContext.Current.Response.StatusCode = (int)System.Net.HttpStatusCode.InternalServerError;
                //return ex.Message;
                throw new WebFaultException<string>(ex.Message, System.Net.HttpStatusCode.InternalServerError);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Adds as new user  
        /// </summary>
        /// <param name="info"></param>
        /// <param name="resourceInfo"></param>
        /// <returns></returns>
        public string AddUser(UserInfo info, ResourceInfo resourceInfo)
        {
            string directory = String.Empty;
            Guid? resourceId = null;
            bool hasImage = false;
            bool hasDirectory = false;
            try
            {
                // Create Directory
                DirectoryManager dirMgr = new DirectoryManager();
                directory = dirMgr.CreateDirectory();

                if (!String.IsNullOrWhiteSpace(directory))
                    hasDirectory = true;

                hasImage = resourceInfo.Data != null;

                if (hasImage)
                {
                    // Add Resource to that directory
                    ResourceManager mgr = new ResourceManager();
                    resourceId = mgr.AddResource(resourceInfo, directory, EnumPostType.Picture);
                }

                using (SqlDataAdapter adapter = new SqlDataAdapter("[user].[AddUserInfo]", AppConfigManager.ConnectionString))
                {
                    adapter.SelectCommand.CommandType = CommandType.StoredProcedure;

                    adapter.SelectCommand.Parameters.AddWithValue("@UserName", info.UserName);
                    adapter.SelectCommand.Parameters.AddWithValue("@DisplayName", info.DisplayName);
                    adapter.SelectCommand.Parameters.AddWithValue("@OwnNumber", info.OwnNumber);
                    adapter.SelectCommand.Parameters.AddWithValue("@GenderId", info.GenderId);
                    adapter.SelectCommand.Parameters.AddWithValue("@AgeRangeId", info.AgeRangeId);
                    adapter.SelectCommand.Parameters.AddWithValue("@Status", info.Status);
                    adapter.SelectCommand.Parameters.AddWithValue("@ImageIdf", resourceId);

                    var nodeParam = adapter.SelectCommand.Parameters.Add("@FolderPath", SqlDbType.Udt);
                    nodeParam.Value = SqlHierarchyId.Parse(directory);
                    nodeParam.UdtTypeName = "HierarchyId";

                    adapter.SelectCommand.Parameters.AddWithValue("@TerritoryId", info.TerritoryId);

                    DataTable dt = new DataTable();

                    // Gets UserId of Inserted User
                    adapter.Fill(dt);

                    if (dt != null && dt.Rows.Count > 0)
                    {
                        var userIdf = dt.Rows[0][CodeHelper.Idf];
                        var userFolderPath = dt.Rows[0][CodeHelper.FolderPath];
                        if (userIdf != null && userFolderPath != null)
                            return JsonWebToken.Encode(new TokenInfo() { Idf = Guid.Parse(userIdf.ToString()), FolderPath = userFolderPath.ToString(), TerritoryId = info.TerritoryId }, CodeHelper.SecretAccessKey, HvHashAlgorithm.RS256);

                    }

                    if (resourceId != null)
                        DeleteFile(resourceId);
                    // Delete directory and file
                    if (hasDirectory)
                        DeleteDirectory(directory);

                    throw new Exception(CodeHelper.UnableToAddUser);
                }
            }
            catch (Exception ex)
            {
                string msg = ex.Message;

                if (resourceId != null)
                    DeleteFile(resourceId);
                // Delete directory and file
                if (hasDirectory)
                    DeleteDirectory(directory);
                throw new Exception(CodeHelper.UnableToAddUser);
            }
        }