示例#1
0
        static Guid? GetList(User u, string body)
        {
            TaskList tasklist = null;

            // a hash indicates a list name to add the new task to
            int index = body.IndexOf("#list:");
            if (index >= 0)
            {
                string listName = body.Substring(index + 1);
                int i = listName.IndexOf('\n');
                if (i > 0)
                {
                    listName = listName.Substring(0, i);
                    listName = listName.Trim();
                    tasklist = TaskStore.TaskLists.FirstOrDefault(tl => tl.UserID == u.ID && tl.Name == listName);
                    if (tasklist != null)
                        return tasklist.ID;
                }
            }

            tasklist = TaskStore.TaskLists.FirstOrDefault(tl => tl.UserID == u.ID && tl.ListTypeID == ToDoListType);
            if (tasklist != null)
                return tasklist.ID;
            else
                return null;
        }
示例#2
0
        string WriteFile(User user, Stream traceStream)
        {
            // Log function entrance
            LoggingHelper.TraceFunction();

            try
            {
                string dir = HttpContext.Current.Server.MapPath(@"~/files");
                // if directory doesn't exist, create the directory
                if (!Directory.Exists(dir))
                    Directory.CreateDirectory(dir);

                DateTime tod = DateTime.Now;
                string filename = String.Format("{0}-{1}.txt",
                    user.Name,
                    tod.Ticks);
                string path = Path.Combine(dir, filename);
                FileStream fs = File.Create(path);
                if (fs == null)
                    return "file not created";

                // copy the trace stream to the output file
                traceStream.CopyTo(fs);
                //fs.Write(bytes, 0, bytes.Length);
                fs.Flush();
                fs.Close();
                return null;
            }
            catch (Exception ex)
            {
                byte[] buffer = new byte[65536];
                int len = traceStream.Read(buffer, 0, buffer.Length);
                string s = Encoding.ASCII.GetString(buffer);
                LoggingHelper.TraceError("Write speech file failed: " + ex.Message);
                return ex.Message;
            }
        }
示例#3
0
        public ActionResult Register(RegisterModel model)
        {
            if (ModelState.IsValid)
            {
                // create the user
                User user = new User() { Name = model.UserName, Password = model.Password, Email = model.Email };

                MembershipCreateStatus createStatus;
                ResourceHelper.CreateUser(new TaskStore(), user, out createStatus);

                if (createStatus == MembershipCreateStatus.Success)
                {
                    FormsAuthentication.SetAuthCookie(model.UserName, false /* createPersistentCookie */);

                    // Log account creation
                    LoggingHelper.TraceInfo("Account created successfully for user " + model.UserName);

                    return RedirectToAction("Index", "TL");
                }
                else
                {
                    ModelState.AddModelError("", ErrorCodeToString(createStatus));

                    // Log account creation failure
                    LoggingHelper.TraceError("Account creation failed: " + ErrorCodeToString(createStatus));
                }
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }
示例#4
0
        public HttpResponseMessageWrapper<User> GetCurrentUser(HttpRequestMessage req)
        {
            User user = null;

            try
            {
                MembershipUser mu = Membership.GetUser();

                // if authenticated by asp.net (meaning, logged in through website), use membership
                // user info.  otherwise get authentication information from message
                if (mu != null)
                {
                    user = new User() { Name = mu.UserName };
                }
                else
                {
                    user = ResourceHelper.GetUserPassFromMessage(req);
                    HttpStatusCode code = ResourceHelper.AuthenticateUser(req, TaskStore);
                    if (code != HttpStatusCode.OK)
                        return new HttpResponseMessageWrapper<User>(req, code);  // user not authenticated
                }
            }
            catch (Exception)
            {
                // membership database is unreachable
                return new HttpResponseMessageWrapper<User>(req, HttpStatusCode.InternalServerError);
            }

            TaskStore taskstore = TaskStore;

            try
            {
                // get the user and all of their top-level objects
                User dbUser = taskstore.Users.
                    Include("ListTypes.Fields").
                    Include("Tags").
                    Include("TaskLists.Tasks.TaskTags").
                    Single<User>(u => u.Name == user.Name);

                // make sure the response isn't cached
                var response = new HttpResponseMessageWrapper<User>(req, dbUser, HttpStatusCode.OK);
                response.Headers.CacheControl = new CacheControlHeaderValue() { NoCache = true };
                return response;
            }
            catch (Exception)
            {
                // couldn't find user - return 404 Not Found
                return new HttpResponseMessageWrapper<User>(req, HttpStatusCode.NotFound);
            }
        }
示例#5
0
        /// <summary>
        /// Create a new user
        /// </summary>
        /// <param name="req">HTTP request</param>
        /// <param name="taskstore">The TaskStore database context</param>
        /// <param name="user">The new user information</param>
        /// <returns>The HTTP status code to return</returns>
        public static HttpStatusCode CreateUser(TaskStore taskstore, User user, out MembershipCreateStatus createStatus)
        {
            // Log function entrance
            LoggingHelper.TraceFunction();

            try
            {
                // create the user using the membership provider
                MembershipUser mu = Membership.CreateUser(user.Name, user.Password, user.Email, null, null, true, null, out createStatus);

                if (createStatus == MembershipCreateStatus.Success)
                {
                    // create the user in the TaskStore user table
                    User u = new User()
                    {
                        ID = (Guid)mu.ProviderUserKey /*Guid.NewGuid()*/,
                        Name = user.Name,
                        Password = user.Password,
                        Email = user.Email
                    };
                    taskstore.Users.Add(u);
                    taskstore.SaveChanges();

                    // Log new user creation
                    LoggingHelper.TraceInfo("Created new user " + user.Name);
                    return HttpStatusCode.Created;
                }
                else
                {
                    // Log failure
                    LoggingHelper.TraceError("Failed to create new user " + user.Name);
                    return HttpStatusCode.Conflict;
                }
            }
            catch (Exception)
            {
                createStatus = MembershipCreateStatus.DuplicateUserName;

                // Log new user creation
                LoggingHelper.TraceError("Failed to create new user " + user.Name);
                return HttpStatusCode.Conflict;
            }
        }
示例#6
0
        string WriteSpeechFile(User user, Stream stream)
        {
            // Log function entrance
            LoggingHelper.TraceFunction();

            try
            {
                //Directory.CreateDirectory(HttpContext.Current.Server.MapPath("~/files"));
                DateTime tod = DateTime.Now;
                string filename = String.Format("{0}{1}-{2}.wav",
                    HttpContext.Current.Server.MapPath("~/files/"),
                    user.Name,
                    tod.Ticks);
                FileStream fs = File.Create(filename);
                if (fs == null)
                    return "file not created";

                stream.CopyTo(fs);
                //byte[] bytes = new byte[32000];
                //int total = 0;
                //int count = stream.Read(bytes, 0, bytes.Length);
                //while (count > 0)
                //{
                //    total += count;
                //    fs.Write(bytes, 0, count);
                //    if (count < bytes.Length)
                //        break;
                //    count = stream.Read(bytes, 0, bytes.Length);
                //}

                // flush and close the file stream
                fs.Flush();
                fs.Close();

                // trace the size of the file
                LoggingHelper.TraceDetail(String.Format("Write speech file: {0} bytes", stream.Position));

                // reset the stream position
                stream.Position = 0;

                return null;
            }
            catch (Exception ex)
            {
                LoggingHelper.TraceError("Write speech file failed: " + ex.Message);
                return ex.Message;
            }
        }