Laravel - Gates Multiple或中间件

时间:2018-03-02 14:43:41

标签: laravel

我在web.php中使用了laravel gate,我在其中添加了中间件,如下所示

const char *

我想添加另一个规则,例如can:admin或can:moderator,因此其中任何一个都是true,gate将允许

任何想法?

2 个答案:

答案 0 :(得分:1)

在为路由启用中间件时,我认为您不能使用逻辑或运算符。

您可以创建第三个Gate,当用户是admin或moderator时返回true。

另一个选择是你要创建一个ProductPolicy。在此策略中,您可以创建一个sellProduct()方法来检查用户是否具有正确的角色:

public function sellProduct(User $user, Product $product) 
{
    // Return true when user is moderator or admin
}

您必须在ProductController@Sell中使用以下方法调用此方法:

$this->authorize('sellProduct', $product);

请注意,门和策略旨在确定用户是否有权执行操作。这可以通过检查用户是否具有某个角色,或者用户是否是产品的所有者来完成。当您将门定义为'admin'时,事情可能会混淆。

答案 1 :(得分:1)


struct TabBar: View {
    
@Binding var selected : Int
@State var expand = true
    
    var drag: some Gesture {
        DragGesture()
            .onChanged {_ in self.expand = false}
        }

var body: some View {
        
    HStack {
        Spacer(minLength:0)
        
        HStack{
            
            if !self.expand{
                
                Button(action: {
                    self.expand.toggle()
                }) {
                    Image("appleLogo")
                        .resizable()
                        .aspectRatio(contentMode: .fill)
                        .frame(width: 15, height: 15, alignment: .center)
                        .foregroundColor(.black)
                        .scaleEffect(2)
                        .padding()
                }
                
            }
                
            else{
                Button(action: {
                    self.selected = 0
                            }) {
                                ZStack {
                                    
                                    Circle()
                                    .frame(width: 40, height: 40)
                                    .opacity(0)
                                    
                                    Image(systemName: "house")
                                        .foregroundColor(self.selected == 0 ? .black : Color("darkGray"))
                                        .padding(.horizontal)
                                        .scaleEffect(1.5)
                                }
                            }
                            
                            Spacer(minLength:15)
                            
                            Button(action: {
                                self.selected = 1
                            }) {
                                ZStack {
                                    
                                    Circle()
                                    .frame(width: 40, height: 40)
                                    .opacity(0)
                                    
                                    Image(systemName: "bubble.left")
                                        .foregroundColor(self.selected == 1 ? .black : Color("darkGray"))
                                        .padding(.horizontal)
                                        .scaleEffect(1.5)
                                }
                            }
                            
                            Spacer(minLength:15)

                            Button(action: {
                                self.selected = 2
                            }) {
                                ZStack {
                                    
                                    Circle()
                                    .frame(width: 40, height: 40)
                                    .opacity(0)
                                    
                                    Image(systemName: "plus.app")
                                        .foregroundColor(self.selected == 2 ? .black : Color("darkGray"))
                                        .padding(.horizontal)
                                        .scaleEffect(1.5)
                                }
                            }
                            
                            Spacer(minLength:15)
                            
                            Button(action: {
                                self.selected = 3
                            }) {
                                ZStack {
                                    
                                    Circle()
                                    .frame(width: 40, height: 40)
                                    .opacity(0)
                                    
                                    Image(systemName: "cart")
                                        .foregroundColor(self.selected == 3 ? .black : Color("darkGray"))
                                        .padding(.horizontal)
                                        .scaleEffect(1.5)
                                }
                            }
                            
                            Spacer(minLength:15)

                            Button(action: {
                                self.selected = 4
                            }) {
                                ZStack {
                                    
                                    Circle()
                                        .frame(width: 40, height: 40)
                                        .opacity(0)
                                    
                                    Image(systemName: "person")
                                        .foregroundColor(self.selected == 4 ? .black : Color("darkGray"))
                                        .padding(.horizontal)
                                        .scaleEffect(1.5)
                                }
                            }
                            
                        }
                    }
        .padding(.vertical,self.expand ? 8 : 6)
        .padding(.horizontal,self.expand ? 20 : 6)
        .background(VisualEffectView())
        .background(Color("TabBarGray").opacity(0.3))
        .opacity(self.expand ? 1.0 : 0.3)
        .clipShape(Capsule())
        .padding(30)
        .onLongPressGesture {
            self.expand.toggle()}
        .gesture(drag)
        .animation(.interactiveSpring(response:0.6, dampingFraction:0.6, blendDuration:0.6))
        .frame(maxWidth: UIScreen.main.bounds.width,
               maxHeight: UIScreen.main.bounds.height, alignment: .center)
                }
            }
        }