public IActionResult Add([FromBody] ResourceDTO resource)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            _rService.AddResource(resource, User.Identity.Name);

            return(Ok());
        }
Beispiel #2
0
        protected override void Seed(SimpleSecurity.AspNetIdentity.Repositories.SecurityContext context)
        {
            //  This method will be called after migrating to the latest version.

            //  You can use the DbSet<T>.AddOrUpdate() helper extension method
            //  to avoid creating duplicate seed data. E.g.
            //
            //    context.People.AddOrUpdate(
            //      p => p.FullName,
            //      new Person { FullName = "Andrew Peters" },
            //      new Person { FullName = "Brice Lambson" },
            //      new Person { FullName = "Rowan Miller" }
            //    );
            //
            var webSecurity = new WebSecurity();

            if (!webSecurity.FoundUser("admin"))
            {
                webSecurity.CreateUserAndAccount("admin", "password", "*****@*****.**");
            }
            if (!webSecurity.FoundUser("user"))
            {
                webSecurity.CreateUserAndAccount("user", "password", "*****@*****.**");
            }
            if (!RoleService.RoleExists("admin"))
            {
                RoleService.AddRole("admin");
                webSecurity.MapUserToRole(webSecurity.GetUserId("admin"), "admin");
            }
            if (!RoleService.RoleExists("user"))
            {
                RoleService.AddRole("user");
                webSecurity.MapUserToRole(webSecurity.GetUserId("user"), "user");
            }

            int AppResourcesFoo = 1;

            if (!ResourceService.FoundResource(AppResourcesFoo))
            {
                //Build the operations available for this resource
                Operations operations = Operations.Read;
                operations |= Operations.Create;
                //Add the resource
                ResourceService.AddResource(AppResourcesFoo, "Foo", operations);
                //Map the resource/operation to the roles
                ResourceService.MapOperationToRole(AppResourcesFoo, Operations.Read, "user");
                ResourceService.MapOperationToRole(AppResourcesFoo, Operations.Create, "admin");
            }
        }
Beispiel #3
0
        public ActionResult Create([Bind(Include = "id,name,description,address,typeid,time")] Resource resource, HttpPostedFileBase file)
        {
            if (customService.SaveFile(file))
            {
                resource.address = file.FileName;
                resource.time    = DateTime.Now;
            }
            if (ModelState.IsValid)
            {
                resourceService.AddResource(resource);
                return(RedirectToAction("Index"));
            }

            return(View(resource));
        }
        protected override void Seed(SecurityContext context)
        {
            WebMatrix.WebData.WebSecurity.InitializeDatabaseConnection("SimpleSecurityConnection",
                                                                       "UserProfile", "UserId", "UserName", autoCreateTables: true);
            var roles      = (WebMatrix.WebData.SimpleRoleProvider)Roles.Provider;
            var membership = (WebMatrix.WebData.SimpleMembershipProvider)Membership.Provider;

            if (!roles.RoleExists("Admin"))
            {
                roles.CreateRole("Admin");
            }
            if (!roles.RoleExists("User"))
            {
                roles.CreateRole("User");
            }
            if (!roles.RoleExists("SuperUser"))
            {
                roles.CreateRole("SuperUser");
            }
            if (!WebSecurity.FoundUser("test"))
            {
                WebSecurity.CreateUserAndAccount("test", "password", "*****@*****.**");
            }
            if (!roles.GetRolesForUser("test").Contains("Admin"))
            {
                roles.AddUsersToRoles(new[] { "test" }, new[] { "admin" });
            }
            if (!WebSecurity.FoundUser("joe"))
            {
                WebSecurity.CreateUserAndAccount("joe", "password", "*****@*****.**");
            }
            if (!roles.GetRolesForUser("joe").Contains("User"))
            {
                roles.AddUsersToRoles(new[] { "joe" }, new[] { "User" });
            }

            //Build the operations available for this resource
            Operations operations = Operations.Read;

            operations |= Operations.Create;
            //Add the resource
            ResourceService.AddResource(AppResources.Foo, "Foo", operations);
            //Map the resource/operation to the roles
            ResourceService.MapOperationToRole(AppResources.Foo, Operations.Read, "User");
            ResourceService.MapOperationToRole(AppResources.Foo, Operations.Create, "Admin");
        }
Beispiel #5
0
 public ActionResult Create([Bind(Include = "id,name,description,address,typeid,time")] Resource resource, HttpPostedFileBase file)
 {
     if (customService.SaveFile(file))
     {
         resource.address = file.FileName;
     }
     if (ModelState.IsValid)
     {
         resource.time = DateTime.Now;
         resourceService.AddResource(resource);
         var id = Request.Cookies["id"].Value.ToString();
         uploadRecordForTeacherService.AddUploadRecordForTeachers(
             new UploadRecordForTeacher()
         {
             sourceId = resource.id, userId = int.Parse(id)
         });
         return(RedirectToAction("Index/" + id));
     }
     return(View(resource));
 }
Beispiel #6
0
        public async Task AddResource_ShouldFillFileUrl()
        {
            BuildMocks();
            var resource = new ResourceDtoTestBuilder()
                           .WithDefaultValues()
                           .WithDummyFile()
                           .Build();
            var userGuid = DefaultValues.Guid;

            var expected = new ResourceEntityTestBuilder()
                           .WithDefaultValues()
                           .WithCreatedById(DefaultValues.Guid)
                           .WithUrl(DefaultValues.Uri.ToString())
                           .Build();

            var addedId = await _resourceService.AddResource(resource, userGuid);

            var actual = _resources.FirstOrDefault(r => r.Id == addedId);

            actual.Should().BeEquivalentTo(expected);
        }
Beispiel #7
0
        public ActionResult Upload(int type)
        {
            string result = "<script>parent.window.uploadCallBack({{success:{0},message:'{1}',path:'{2}',type:" + type + "}})</script>";

            if (type < 1 || type > 4)
            {
                return(Content(string.Format(result, "false", "附件类型错误", "")));
            }
            string allowExt   = string.Empty;
            string directName = string.Empty;

            switch (type)
            {
            case 1:
                directName = "bgpic";
                allowExt   = ".jpg|.gif|.png";
                break;

            case 2:
                directName = "pagepic";
                allowExt   = ".jpg|.gif|.png";
                break;

            case 3:
                directName = "audio";
                allowExt   = ".mp3";
                break;

            case 4:
                directName = "vedio";
                allowExt   = ".mp4";
                break;
            }
            StringBuilder          strMsg = new StringBuilder();
            HttpFileCollectionBase fileList = Request.Files;
            HttpPostedFileBase     postedFile = fileList[0];
            string fileName, fileExtension;

            fileName = System.IO.Path.GetFileName(postedFile.FileName);
            if (string.IsNullOrEmpty(fileName))
            {
                return(Content(string.Format(result, "false", "上传文件为空", "")));
            }
            fileExtension = System.IO.Path.GetExtension(fileName);

            if (allowExt.IndexOf(fileExtension) == -1)
            {
                return(Content(string.Format(result, "false", "不允许的附件类型", "")));
            }
            //strMsg.Append("上传的文件类型:" + postedFile.ContentType.ToString() + "<br>");
            //strMsg.Append("客户端文件地址:" + postedFile.FileName + "<br>");
            //strMsg.Append("上传文件的文件名:" + fileName + "<br>");
            //strMsg.Append("上传文件的扩展名:" + fileExtension + "<br><hr>");

            ///'可根据扩展名字的不同保存到不同的文件夹
            ///注意:可能要修改你的文件夹的匿名写入权限
            var newfileName = Guid.NewGuid() + fileExtension;
            var path = System.Web.HttpContext.Current.Request.MapPath("/Upload/" + directName + "/");

            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            var filePath = path + newfileName;
            var virtualPath = "/Upload/" + directName + "/" + newfileName;

            try
            {
                postedFile.SaveAs(filePath);
                ResourceService.AddResource(new Models.Resource()
                {
                    CreateTime = DateTime.Now,
                    Type       = type,
                    Creator    = "",
                    IsDelete   = false,
                    Name       = fileName,
                    Path       = virtualPath,
                    Tip        = ""
                });
                return(Content(string.Format(result, "true", "", virtualPath)));
            }
            catch (Exception ex)
            {
                return(Content(string.Format(result, "false", ex.Message, "")));
            }
        }