public async Task<ActionResult> SetUp(SetupViewModel model)
        {
            if (ModelState.IsValid)
            {
                using (var db = new FooBoxContext())
                using (var transaction = db.Database.BeginTransaction())
                {
                    try
                    {
                        using (var fileManager = new FileManager(db))
                        using (var userManager = new UserManager(db))
                        {
                            userManager.InitialSetup();
                            fileManager.InitialSetup();

                            // Create the admin user.
                            userManager.CreateUser(new User { Name = model.AdminUserName, QuotaLimit = long.MaxValue }, model.AdminPassword);
                            userManager.FindUser(model.AdminUserName).Groups.Add(userManager.FindGroup(UserManager.AdministratorsGroupName));
                            await db.SaveChangesAsync();
                        }

                        transaction.Commit();

                        return RedirectToAction("Index", "Home");
                    }
                    catch (Exception ex)
                    {
                        ModelState.AddModelError("", "Error setting up the database: " + ex.Message);
                    }
                }
            }

            // Error
            return View(model);
        }
        public ActionResult ClientLogin(string userName, string password, string clientName)
        {
            var user = UserManager.FindUser(userName, password);
            if (user != null) {
                using (var f = new FileManager(UserManager.Context))
                {
                    Client c = f.CreateClient(user.Id, clientName);

                    return Json(new ClientLoginResult
                    {
                        Id = c.Id,
                        Secret = c.Secret,
                        UserId = c.UserId
                    });
                }
            }
            return Content("fail");
        }
        public static void UploadBlob(FileManager fileManager, Client client, Stream stream, out string hash, out long size)
        {
            var clientUploadDirectory = fileManager.AccessClientUploadDirectory(client.Id);
            var randomName = Utilities.GenerateRandomString(Utilities.IdChars, 32);
            var tempUploadFileName = clientUploadDirectory.FullName + "\\" + randomName;

            byte[] buffer = new byte[4096 * 4];
            int bytesRead;
            long totalBytesRead = 0;

            // Simultaneously hash the file and write it out to a temporary file.

            using (var hashAlgorithm = fileManager.CreateBlobHashAlgorithm())
            {
                using (var fileStream = new FileStream(tempUploadFileName, FileMode.Create))
                {
                    while ((bytesRead = stream.Read(buffer, 0, buffer.Length)) != 0)
                    {
                        hashAlgorithm.TransformBlock(buffer, 0, bytesRead, null, 0);
                        fileStream.Write(buffer, 0, bytesRead);
                        totalBytesRead += bytesRead;
                    }
                }

                hashAlgorithm.TransformFinalBlock(new byte[0], 0, 0);
                hash = (new SoapHexBinary(hashAlgorithm.Hash)).ToString();

                try
                {
                    System.IO.File.Move(tempUploadFileName, clientUploadDirectory.FullName + "\\" + hash);
                }
                catch
                {
                    // We're going to assume that the file with hash as its name already exists.
                    // This means that someone has already uploaded an identical file.
                    System.IO.File.Delete(tempUploadFileName);
                }
            }

            size = totalBytesRead;
        }
 public ClientController()
 {
     _fileManager = new FileManager();
     _userManager = new UserManager(_fileManager.Context);
 }
        public User CreateUser(User template, string password)
        {
            byte[] saltBytes = new byte[16];
            (new Random()).NextBytes(saltBytes);
            string salt = (new SoapBase64Binary(saltBytes)).ToString();
            User newUser = new User
            {
                Name = template.Name,
                PasswordHash = ComputePasswordHash(salt, password),
                PasswordSalt = salt,
                FirstName = template.FirstName,
                LastName = template.LastName,
                QuotaLimit = template.QuotaLimit
            };

            try
            {
                _context.Users.Add(newUser);
                _context.SaveChanges();

                using (var fileManager = new FileManager(_context))
                {
                    // Create the user's root folder.
                    fileManager.CreateUserRootFolder(newUser);
                    // Create the user's default internal client.
                    fileManager.CreateClient(newUser.Id, "Internal", FileManager.InternalClientTag);
                }
            }
            catch
            {
                return null;
            }

            return newUser;
        }
Exemple #6
0
 public InvitationController()
 {
     _fileManager = new FileManager();
     _userManager = new UserManager(_fileManager.Context);
 }