PageRenderTime 60ms CodeModel.GetById 45ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/Drivers/S3StorageProviderSettingsPartDriver.cs

#
C# | 84 lines | 71 code | 11 blank | 2 comment | 20 complexity | e209c1a68e35341403cb6144c369a759 MD5 | raw file
 1using System;
 2using System.Linq;
 3using Amazon.S3;
 4using Amazon.S3.Model;
 5using Amazon.S3.Util;
 6using Werul.S3StorageProvider.Models;
 7using Orchard.ContentManagement;
 8using Orchard.ContentManagement.Drivers;
 9using Orchard.Localization;
10
11namespace Werul.S3StorageProvider.Drivers
12{
13    public class S3StorageProviderSettingsPartDriver : ContentPartDriver<S3StorageProviderSettingsPart> {
14        public S3StorageProviderSettingsPartDriver()
15        {
16            T = NullLocalizer.Instance;
17        }
18
19        public Localizer T { get; set; }
20
21        protected override string Prefix { get { return "S3StorageProviderSettings"; } }
22
23        protected override DriverResult Editor(S3StorageProviderSettingsPart part, dynamic shapeHelper)
24        {
25            return Editor(part, null, shapeHelper);
26        }
27
28        protected override DriverResult Editor(S3StorageProviderSettingsPart part, IUpdateModel updater, dynamic shapeHelper)
29        {
30
31            return ContentShape("Parts_S3StorageProvider_SiteSettings", () =>
32            {
33                    if (updater != null && updater.TryUpdateModel(part, Prefix, null, null)) {
34                        ValidateS3Connection(part, updater);
35                    }
36                    return shapeHelper.EditorTemplate(TemplateName: "Parts.S3StorageProvider.SiteSettings", Model: part.Record, Prefix: Prefix); 
37                })
38                .OnGroup("Amazon S3");
39        }
40
41        private void ValidateS3Connection(S3StorageProviderSettingsPart part, IUpdateModel updater) 
42        {
43            var s3Config = new AmazonS3Config() {
44                ServiceURL = "s3.amazonaws.com",
45                CommunicationProtocol = Amazon.S3.Model.Protocol.HTTP,
46            };
47
48            try {
49                // Check AWS credentials, bucket name and bucket permissions
50                string bucketName = part.Record.BucketName;
51
52                using (var client = Amazon.AWSClientFactory.CreateAmazonS3Client(part.Record.AWSAccessKey, part.Record.AWSSecretKey, s3Config)) {
53                    if (!AmazonS3Util.DoesS3BucketExist(bucketName, client)) {
54                        updater.AddModelError("Settings", T("Invalid bucket name. No bucket by the name {0} exists.", part.Record.BucketName));
55                    } else {
56                        // Check for read/write permissions
57                        var acl = client.GetACL(new GetACLRequest() {
58                            BucketName = bucketName
59                        });
60
61                        var grants = acl.AccessControlList.Grants;
62
63                        if (!grants.Any(x => x.Permission == S3Permission.FULL_CONTROL)) {
64                            if (!grants.Any(x => x.Permission == S3Permission.WRITE)) {
65                                updater.AddModelError("Settings", T("You don't have write access to this bucket"));
66                            }
67                            if (!grants.Any(x => x.Permission == S3Permission.READ)) {
68                                updater.AddModelError("Settings", T("You don't have read access to this bucket"));
69                            }
70                        }
71                    }
72                }
73            } catch (AmazonS3Exception ex) {
74                if (ex.ErrorCode != null && (ex.ErrorCode.Equals("InvalidAccessKeyId") || ex.ErrorCode.Equals("InvalidSecurity"))) {
75                    updater.AddModelError("Settings", T("Invalid AWS credentials"));
76                } else if (ex.ErrorCode != null && ex.ErrorCode.Equals("AccessDenied")) {
77                    updater.AddModelError("Settings", T("Access denied. You don't have permission to access the bucket '{0}'", part.Record.BucketName));
78                } else {
79                    updater.AddModelError("Settings", T("Unknown error: {0}", ex.Message));
80                }
81            }
82        }
83    }
84}